reviveError

Revive a JSON-serialized error object.

Usage

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

reviveError( key, value )

Revives a JSON-serialized error object.

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

var err = JSON.parse( str, reviveError );
// returns <TypeError>

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

Notes

Examples

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 );
/* returns
    {
        "type": "SyntaxError",
        "name": "SyntaxError",
        "message": "bad syntax",
        "stack": "..."
    }
*/

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

var err2 = JSON.parse( str, reviveError );
// returns <SyntaxError>

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

bool = ( err1.stack === err2.stack );
// returns true