Socket
Socket
Sign inDemoInstall

jsonify-error

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonify-error

Get a plain object fully representing the error for better logs and server responses.


Version published
Weekly downloads
1.6K
increased by13.08%
Maintainers
1
Weekly downloads
 
Created
Source

jsonify-error

npm package

NPM version License NPM downloads Dependency Status Dev Dependency Status Open Issues Closed Issues contributions welcome jsDelivr hits

It's 2018 and neither JSON.stringify(e) nor console.log(e) behave as nicely as they could when e is an error.

With jsonify-error, use jsonifyError(e) instead of e. It produces a plain object with everything one could wish to see about an error.

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-n9I+URPQ8PzrWft0T/DU2koP/WJ1R56t1OGcaWwUaBFrPLkH63jiI3P+vVolz14b" 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

In Node

In node, as usual, simply do:

npm install --save jsonify-error

Example result

The resulting plain object has the form:

{
    "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)"
    ]
}

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));
    // process.exit(1); // Exiting or not depends on your situation
});

Example usage: with express

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));
});

Example usage: overriding console

require("jsonify-error").overrideConsole();
// Now console.log, console.warn and console.error automatically
// call jsonifyError() on each argument that is instanceof Error
// before logging. Note that overriding native functions/objects
// is usually not a good practice so use this with caution.

Note: since 1.2.0, you can simply console.log(jsonifyError(anything)) if you prefer, because if anything is not an error, jsonifyError will not touch it at all.

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

Keywords

FAQs

Package last updated on 15 Aug 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc