Revive a JSON-serialized error object.
npm install @stdlib/error-reviver
Alternatively,
- To load the package in a website via a
script
tag without installation and bundlers, use the ES Module available on theesm
branch. - If you are using Deno, visit the
deno
branch. - For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the
umd
branch.
The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.
var reviveError = require( '@stdlib/error-reviver' );
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
.
-
Supported built-in
error
types:
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
@stdlib/error/to-json
: return a JSON representation of an error object.
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2022. The Stdlib Authors.