jsonify-error
It's 2018 and neither JSON.stringify(x)
nor console.log(x)
behave as nicely as they could when x
is an error.
With jsonify-error, use jsonifyError(x)
instead of x
. It produces a plain object with everything one could wish to see about an error.
Installation
For browsers, simply include browser.js in your entry point:
<script src="https://rawgit.com/papb/jsonify-error/1.0.0/browser.js"></script>
In node, as usual, simply do:
npm install --save jsonify-error
Example result
The resulting plain object has the form:
{
"name": "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)"
]
}
Example usage: try-catch
var jsonifyError = require("jsonify-error");
try {
} catch (e) {
console.error(jsonifyError(e));
process.exit(1);
}
Example usage: promises
For better error logs of unhandled errors in promises, the recommended solution is to use the sibling module, better-promise-error-log. But if you insist, you can do:
var jsonifyError = require("jsonify-error");
somethingAsync().then(() => {
}).catch(error => {
console.error(jsonifyError(e));
});
Example usage: with express
var jsonifyError = require("jsonify-error");
app.get('/your/api', (req, res) => {
res.status(500).json(jsonifyError(error));
});
Example usage: overriding console
require("jsonify-error").overrideConsole();
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.
License
MIT - Pedro Augusto de Paula Barbosa