reviveError

Revive a JSON-serialized error object.

Usage

var reviveError = require( '@stdlib/error/reviver' );

reviveError( key, value )

Revives a JSON-serialized error object.

var parseJSON = require( '@stdlib/utils/parse-json' );

var str = '{"type":"TypeError","message":"beep"}';

var err = parseJSON( str, reviveError );
// returns <TypeError>

For details on the JSON serialization format, see @stdlib/error/to-json.

Notes

Examples

var parseJSON = require( '@stdlib/utils/parse-json' );
var err2json = require( '@stdlib/error/to-json' );
var reviveError = require( '@stdlib/error/reviver' );

var err1 = new SyntaxError( 'bad syntax' );
// returns <SyntaxError>

var json = err2json( err1 );
/* e.g., returns
    {
        'type': 'SyntaxError',
        'name': 'SyntaxError',
        'message': 'bad syntax',
        'stack': '...'
    }
*/

var str = JSON.stringify( json );
// e.g., returns '{"type":"SyntaxError","name":"SyntaxError","message":"bad syntax","stack":"..."}'

var err2 = parseJSON( str, reviveError );
// returns <SyntaxError>

var bool = ( err1.message === err2.message );
// returns true

bool = ( err1.stack === err2.stack );
// returns true
Did you find this page helpful?