format-error
formats an error for display
Last updated 6 years ago by smassa .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install format-error 
SYNC missed versions from official npm registry.

format-error

Formats an error object such that:

  • message is ignored; assumes it's in the stack
  • full stack is displayed
  • other properties on object are displayed
  • nested errors are displayed

install

npm install --save format-error

example

var format = require('format-error').format;

var error = new Error('something broke');
error.inner = new Error('some inner thing broke');
error.code = '500c';
error.severity = 'high';

var message = format(error);

console.error(message);

/*
Error:    something broke
    at Object.<anonymous> (/home/sean/demo/testium/err.js:28:13)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
inner:
  Error: some inner thing broke
    at Object.<anonymous> (/home/sean/demo/testium/err.js:29:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3
code:     500c
severity: high
*/

api

format(error, prettyjsonOptions)

Formats an error for output. error is an Error object. prettyjsonOptions (optional) is passed directly to prettyjson.

patchError()

Permanently patches the error object such that serialization (toJSON calls) behave according to the goals of this module. This is not required unless you want JSON.stringify(error) to act somewhat like format(error) in cases where you can't control how the output is formatted directly.

This module only temporarily patches Error in order to produce the formatted output.

This can be undone by calling unpatchError().

unpatchError()

This reverts changes made to Error.prototype.toJSON by patchError().

This should only be called if you explictly called patchError.

License

MIT

Current Tags

  • 1.0.0                                ...           latest (6 years ago)

1 Versions

  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 2
Last Month 5
Dependencies (1)
Dev Dependencies (2)

Copyright 2014 - 2016 © taobao.org |