Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

error-stringify

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

error-stringify

Augment errors with `Error#toJSON` that preserves `name`, `message`, `stack` as well as any other normally stringified attribute.

  • 0.9.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

error-stringify

Augment errors with Error#toJSON that preserves name, message, stack as well as any other normally stringified attribute.

Motive (use-cases)

  • As a Developer
  • I would like to preserve error attributes in JSON stringification
  • So that I can presever the props above while I:
    • output errors in loggers with log.warn("error %j", error)
    • dump errors to disk
    • stringify errors and send them over the network (to couchdb, mongo, log-stash, or any other service)

Features:

  • control which errors should include this behavior: all errors, or only my custom errors depend what target I pass
  • control wether the stack trace should be returned raw, or split by new line (the later is useful for improving readability of stack traces in Kibana and such)
  • control wether the stringification should include only properties from the stringified instance or all enumerable properties available via the prototype chain. (the later is useful when your custom errors inheritence tree defines names, categories/tags and codes on the prototype)
  • optimized for minimal memory footprint and high performance:
    • augments with specific slim optimized implementations that match your configs(instead of one with logic inside)
    • does not declare any symbols in any closure, except module.export

Usage:

The usage vary between the following two extremes. Pass what you need to control the behavior :)

applying default behavior to all errors

require('...')()

Default behavior does not split stack traces, and includes only properties on the passed instance (enumerable or not).

applying configured behavior to custom errors

var AppError = require('./app-error-base');
require('...')({
    target            : AppError
    splitStackTrace   : true, 
    includeProtoChain : true
})

which is the equivalent of:

var AppError = require('./app-error-base');
var options  = {
    splitStackTrace   : true, 
    includeProtoChain : true
};
require('...')(options, AppError)

The later form is useful when you want to pick the options from a file instead of hardcoding it as a literal.

Installation

npm install --save {}

Alternatives I looked into:

  • (stringify-error)[https://www.npmjs.com/package/stringify-error]

  • (error-tojson)[https://www.npmjs.com/package/error-tojson]

  • (utils-error-to-json)[https://github.com/kgryte/utils-error-to-json]

Lisence

MIT, and that's it

Happy coding :)

FAQs

Package last updated on 28 Mar 2016

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