Gathering detailed insights and metrics for jsonify-error
Gathering detailed insights and metrics for jsonify-error
Gathering detailed insights and metrics for jsonify-error
Gathering detailed insights and metrics for jsonify-error
Convert errors to JSON or to a good string. Develop faster with better error messages.
npm install jsonify-error
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
5 Stars
95 Commits
1 Forks
1 Watchers
1 Branches
2 Contributors
Updated on Apr 04, 2020
Latest Version
2.0.0
Package Id
jsonify-error@2.0.0
Unpacked Size
77.74 kB
Size
20.77 kB
File Count
22
NPM Version
6.8.0
Node Version
10.15.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
Convert errors to JSON or to a good string. Develop faster with better error messages.
It's 2019 and still the default behavior of JavaScript could be better with regard to displaying/manipulating errors:
JSON.stringify(e)
: Bade.toString()
: Bade.toJSON()
: Doesn't existconsole.log(e)
: Bad in browsers, not so bad in Node but could be betterBut jsonify-error comes to the rescue:
JSON.stringify(e)
:
JSON.stringify(jsonifyError(e))
insteadjsonifyError.overrideErrorMethods()
once and then JSON.stringify(e)
will work.e.toString()
:
jsonifyError.asString(e)
insteadjsonifyError.overrideErrorMethods()
once and then e.toString()
will work.e.toJSON()
:
jsonifyError(e)
insteadjsonifyError.overrideErrorMethods()
once and then e.toJSON()
will work.console.log(e)
:
jsonifyError.log(e)
insteadjsonifyError.overrideConsole()
once and then console.log(e)
will work.For browsers, simply include one of the dists in your entry point, such as dist/jsonify-error.js
. The dists are available in jsDelivr:
1<script src="https://cdn.jsdelivr.net/npm/jsonify-error@2.0.0/dist/jsonify-error.min.js" integrity="sha384-k3Is8aV5PW6XO2NtZyFbjgZLKNWv4kFrtuN0cnOhaw+qKurzZIlOZZNmih+HGKpN" crossorigin="anonymous"></script>
The following dists are available (with source maps):
dist/jsonify-error.js
dist/jsonify-error.min.js
dist/jsonify-error.es5.js
dist/jsonify-error.es5.min.js
Or if you're developing a browser library with Browserify, you can just require it normally, as if you were in a Node environment.
In node, as usual, simply do:
npm install --save jsonify-error
The main purpose of jsonify-error, as the name suggests, is to convert an error to a plain object. Just do jsonifyError(e)
and you will get something like:
1{ 2 "name": "TypeError", 3 "className": "TypeError", 4 "message": "It can't be a string", 5 "superclasses": ["Error", "Object"], 6 "enumerableFields": { 7 // If the error has other fields they appear here (including in the prototype chain): 8 "someField": "someValue" 9 }, 10 "stack": [ 11 "TypeError: It can't be a string", 12 "at z (E:\\test.js:15:15)", 13 "at E:\\test.js:10:9", 14 "at Array.forEach (native)", 15 "at y (E:\\test.js:9:13)", 16 "at x (E:\\test.js:5:5)", 17 "at w (E:\\test.js:24:9)", 18 "at Object.<anonymous> (E:\\test.js:32:1)", 19 "at Module._compile (module.js:570:32)", 20 "at Object.Module._extensions..js (module.js:579:10)", 21 "at Module.load (module.js:487:32)" 22 ] 23}
If you're thinking "Great! Now I can do console.log(jsonifyError(e))
instead of console.log(e)
" in a browser, you're in the right track, but you can do even better!
A few utility methods are exposed by jsonifyError beyond the main one, as mentioned in the beginning of this README.
jsonifyError.log(e)
: Logs the error in a much better way than console.log(e)
.jsonifyError.overrideConsole()
: Makes console.log
, console.warn
, console.error
work like jsonifyError.log
automatically. Calling this once is enough.jsonifyError.overrideErrorMethods()
: Heavily improves e.toString()
and adds e.toJSON()
to all errors automatically. Calling this once is enough.1const jsonifyError = require("jsonify-error"); 2 3try { 4 // ... 5} catch (e) { 6 jsonifyError.log(e); 7 // ... 8}
1const jsonifyError = require("jsonify-error"); 2 3somethingAsync().then(() => { 4 // ... 5}).catch(error => { 6 jsonifyError.log(e); 7 // ... 8});
Also, for promises, there is a sibling module called better-promise-error-log which takes care of showing the improved logs automatically for unhandled rejections.
1var jsonifyError = require("jsonify-error"); 2 3app.get('/your/api', (req, res) => { 4 // ... 5 // Instead of res.status(500).json(error), do: 6 res.status(500).json(jsonifyError(error)); 7});
Note: if you've overriden error methods (by calling jsonifyError.overrideErrorMethods()
), the above can be simplified to res.status(500).json(error)
(see the overriding methods section).
1const jsonifyError = require("jsonify-error"); 2jsonifyError.overrideConsole(); 3jsonifyError.overrideErrorMethods(); 4// Now `console.log`, `console.warn` and `console.error` will be much better. 5// Also, `e.toString()` will be much better and `e.toJSON()` will be available.
Any contribution is very welcome. Feel free to open an issue about anything: questions, suggestions, feature requests, bugs, improvements, mistakes, whatever. I will be always looking.
The changelog is available in CHANGELOG.md.
MIT (c) Pedro Augusto de Paula Barbosa
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
Project has not signed or included provenance with any releases.
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More