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
jsonify
JSON without touching any globals
fast-json-stable-stringify
deterministic `JSON.stringify()` - a faster version of substack's json-stable-strigify without jsonify
@types/json-stable-stringify-without-jsonify
TypeScript definitions for json-stable-stringify-without-jsonify
json-stable-stringify-without-jsonify
deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results, with no public domain dependencies
npm install jsonify-error
75.2
Supply Chain
99
Quality
75.5
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
5 Stars
95 Commits
1 Forks
2 Watching
1 Branches
2 Contributors
Updated on 04 Apr 2020
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-6.6%
569
Compared to previous day
Last week
113.1%
2,838
Compared to previous week
Last month
45%
7,710
Compared to previous month
Last year
31.9%
63,301
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>
They are also available as GitHub release assets (since 1.4.2). The following formats are available (with source maps):
jsonify-error.js
jsonify-error.min.js
(minified)jsonify-error.es5.js
(ES5 compatible)jsonify-error.es5.min.js
(ES5 compatible, minified)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
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
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 2024-11-18
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