• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# err-code
2
3[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]
4
5[npm-url]:https://npmjs.org/package/err-code
6[downloads-image]:http://img.shields.io/npm/dm/err-code.svg
7[npm-image]:http://img.shields.io/npm/v/err-code.svg
8[travis-url]:https://travis-ci.org/IndigoUnited/js-err-code
9[travis-image]:http://img.shields.io/travis/IndigoUnited/js-err-code/master.svg
10[david-dm-url]:https://david-dm.org/IndigoUnited/js-err-code
11[david-dm-image]:https://img.shields.io/david/IndigoUnited/js-err-code.svg
12[david-dm-dev-url]:https://david-dm.org/IndigoUnited/js-err-code#info=devDependencies
13[david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/js-err-code.svg
14
15Create new error instances with a code and additional properties.
16
17
18## Installation
19
20`$ npm install err-code` - `NPM`
21`$ bower install err-code` - `bower`
22
23The browser file is named index.umd.js which supports CommonJS, AMD and globals (errCode).
24
25
26## Why
27
28I find myself doing this repeatedly:
29
30```js
31var err = new Error('My message');
32err.code = 'SOMECODE';
33err.detail = 'Additional information about the error';
34throw err;
35```
36
37
38## Usage
39
40Simple usage.
41
42```js
43var errcode = require('err-code');
44
45// fill error with message + code
46throw errcode(new Error('My message'), 'ESOMECODE');
47// fill error with message + code + props
48throw errcode(new Error('My message'), 'ESOMECODE', { detail: 'Additional information about the error' });
49// fill error with message + props
50throw errcode(new Error('My message'), { detail: 'Additional information about the error' });
51
52
53// You may also pass a string in the first argument and an error will be automatically created
54// for you, though the stack trace will contain err-code in it.
55
56// create error with message + code
57throw errcode('My message', 'ESOMECODE');
58// create error with message + code + props
59throw errcode('My message', 'ESOMECODE', { detail: 'Additional information about the error' });
60// create error with message + props
61throw errcode('My message', { detail: 'Additional information about the error' });
62```
63
64
65## Tests
66
67`$ npm test`
68
69
70## License
71
72Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
73