Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
jsonify-error
Advanced tools
Convert errors to JSON or to a good string. Develop faster with better error messages.
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:
<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:
{
"name": "TypeError",
"className": "TypeError",
"message": "It can't be a string",
"superclasses": ["Error", "Object"],
"enumerableFields": {
// If the error has other fields they appear here (including in the prototype chain):
"someField": "someValue"
},
"stack": [
"TypeError: It can't be a string",
"at z (E:\\test.js:15:15)",
"at E:\\test.js:10:9",
"at Array.forEach (native)",
"at y (E:\\test.js:9:13)",
"at x (E:\\test.js:5:5)",
"at w (E:\\test.js:24:9)",
"at Object.<anonymous> (E:\\test.js:32:1)",
"at Module._compile (module.js:570:32)",
"at Object.Module._extensions..js (module.js:579:10)",
"at Module.load (module.js:487:32)"
]
}
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.const jsonifyError = require("jsonify-error");
try {
// ...
} catch (e) {
jsonifyError.log(e);
// ...
}
const jsonifyError = require("jsonify-error");
somethingAsync().then(() => {
// ...
}).catch(error => {
jsonifyError.log(e);
// ...
});
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.
var jsonifyError = require("jsonify-error");
app.get('/your/api', (req, res) => {
// ...
// Instead of res.status(500).json(error), do:
res.status(500).json(jsonifyError(error));
});
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).
const jsonifyError = require("jsonify-error");
jsonifyError.overrideConsole();
jsonifyError.overrideErrorMethods();
// Now `console.log`, `console.warn` and `console.error` will be much better.
// 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
FAQs
Convert errors to JSON or to a good string. Develop faster with better error messages.
The npm package jsonify-error receives a total of 1,345 weekly downloads. As such, jsonify-error popularity was classified as popular.
We found that jsonify-error demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.