jsonify-error
Convert errors to JSON or to a good string. Develop faster with better error messages.
It's 2018 and still the default behavior of JavaScript could be better with regard to displaying errors:
console.log(e)
: BadJSON.stringify(e)
: Bade.toString()
: Bade.toJSON()
: Doesn't exist
But jsonify-error comes to the rescue:
- For
console.log(e)
: Use jsonifyError.log(e)
instead (or call jsonifyError.overrideConsole()
once and then console.log(e)
will work too). - For
JSON.stringify(e)
: Use JSON.stringify(jsonifyError(e))
instead (or call jsonifyError.overrideErrorMethods()
once and then of JSON.stringify(e)
will work too). - For
e.toString()
: Call jsonifyError.overrideErrorMethods()
once and e.toString()
will work. - For
e.toJSON()
: Use jsonifyError(e)
instead (or call jsonifyError.overrideErrorMethods()
once and e.toJSON()
will work).
Installation
In Browsers
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@1.3.0/dist/jsonify-error.js" integrity="sha384-IlFtQEeOfO1Uw6QCHwhpQFXZE7CghDHsPkCgjjHiMZmzEYBNQV3UmBpfulfxC/QJ" 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("jsonify-error")
normally.
In Node
In node, as usual, simply do:
npm install --save jsonify-error
What it does
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": {
"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)
", 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.
Example: with try-catch blocks
const jsonifyError = require("jsonify-error");
try {
} catch (e) {
jsonifyError.log(e);
process.exit(1);
}
Example: with promises
var 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.
Example: with express
var jsonifyError = require("jsonify-error");
app.get('/your/api', (req, res) => {
res.status(500).json(jsonifyError(error));
});
Example usage: overriding methods
const jsonifyError = require("jsonify-error");
jsonifyError.overrideConsole();
jsonifyError.overrideErrorMethods();
Contributing
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.
Changelog
The changelog is available in CHANGELOG.md.
License
MIT (c) Pedro Augusto de Paula Barbosa