[6a3a178] | 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] [![Greenkeeper badge][greenkeeper-image]][greenkeeper-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?type=dev
|
---|
| 13 | [david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/js-err-code.svg
|
---|
| 14 | [greenkeeper-image]:https://badges.greenkeeper.io/IndigoUnited/js-err-code.svg
|
---|
| 15 | [greenkeeper-url]:https://greenkeeper.io/
|
---|
| 16 |
|
---|
| 17 | Create new error instances with a code and additional properties.
|
---|
| 18 |
|
---|
| 19 |
|
---|
| 20 | ## Installation
|
---|
| 21 |
|
---|
| 22 | ```console
|
---|
| 23 | $ npm install err-code
|
---|
| 24 | // or
|
---|
| 25 | $ bower install err-code
|
---|
| 26 | ```
|
---|
| 27 |
|
---|
| 28 | The browser file is named index.umd.js which supports CommonJS, AMD and globals (errCode).
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | ## Why
|
---|
| 32 |
|
---|
| 33 | I find myself doing this repeatedly:
|
---|
| 34 |
|
---|
| 35 | ```js
|
---|
| 36 | var err = new Error('My message');
|
---|
| 37 | err.code = 'SOMECODE';
|
---|
| 38 | err.detail = 'Additional information about the error';
|
---|
| 39 | throw err;
|
---|
| 40 | ```
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | ## Usage
|
---|
| 44 |
|
---|
| 45 | Simple usage.
|
---|
| 46 |
|
---|
| 47 | ```js
|
---|
| 48 | var errcode = require('err-code');
|
---|
| 49 |
|
---|
| 50 | // fill error with message + code
|
---|
| 51 | throw errcode(new Error('My message'), 'ESOMECODE');
|
---|
| 52 | // fill error with message + code + props
|
---|
| 53 | throw errcode(new Error('My message'), 'ESOMECODE', { detail: 'Additional information about the error' });
|
---|
| 54 | // fill error with message + props
|
---|
| 55 | throw errcode(new Error('My message'), { detail: 'Additional information about the error' });
|
---|
| 56 | ```
|
---|
| 57 |
|
---|
| 58 | ## Pre-existing fields
|
---|
| 59 |
|
---|
| 60 | If the passed `Error` already has a `.code` field, or fields specified in the third argument to `errcode` they will be overwritten, unless the fields are read only or otherwise throw during assignment in which case a new object will be created that shares a prototype chain with the original `Error`. The `.stack` and `.message` properties will be carried over from the original error and `.code` or any passed properties will be set on it.
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 | ## Tests
|
---|
| 64 |
|
---|
| 65 | `$ npm test`
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | ## License
|
---|
| 69 |
|
---|
| 70 | Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
|
---|