|
1 |
| -JSON API Errors |
2 |
| -=============== |
| 1 | +# JSON API Errors |
| 2 | + |
| 3 | +Create errors using [JSON API errors format](http://jsonapi.org/format/#errors) |
| 4 | + |
| 5 | +[][package] |
| 6 | + |
| 7 | + |
| 8 | +[][version] |
| 9 | +[][build] |
| 10 | +[][license] |
| 11 | +[][climate] |
| 12 | +[][coverage] |
| 13 | + |
| 14 | +### Installation |
| 15 | + |
| 16 | +`$ npm install json-api-errors` |
| 17 | + |
| 18 | +### Usage |
| 19 | + |
| 20 | +#### `Errors.createSingle(id, message, [meta])` |
| 21 | + |
| 22 | +Create a single errors object. |
| 23 | + |
| 24 | +- `id` (String) |
| 25 | + - required |
| 26 | +- `message` (String) |
| 27 | + - required |
| 28 | +- `meta` (Object | String) |
| 29 | + - optional |
| 30 | + |
| 31 | +```javascript |
| 32 | +const Errors = require('json-api-errors') |
| 33 | + |
| 34 | +var errors = new Errors() |
| 35 | +var err = errors.createSingle('CUSTOM_ERROR', 'Custom error message') |
| 36 | + |
| 37 | +console.log(err) |
| 38 | +// { |
| 39 | +// errors: [{ id: 'CUSTOM_ERROR', message: 'Custom error message' }] |
| 40 | +// } |
| 41 | +``` |
| 42 | + |
| 43 | +#### `Errors.add(id, message, [meta])` |
| 44 | + |
| 45 | +Add an error to the errors array |
| 46 | + |
| 47 | +- `id` (String) |
| 48 | + - required |
| 49 | +- `message` (String) |
| 50 | + - required |
| 51 | +- `meta` (Object | String) |
| 52 | + - optional |
| 53 | + |
| 54 | +```javascript |
| 55 | +const Errors = require('json-api-errors') |
| 56 | + |
| 57 | +var errors = new Errors() |
| 58 | +errors.add('CUSTOM_ERROR', 'Custom error message') |
| 59 | +``` |
| 60 | + |
| 61 | +#### `Errors.get()` |
| 62 | + |
| 63 | +Retreive all errors. Errors are returned in a JSON format |
| 64 | + |
| 65 | +```javascript |
| 66 | +const Errors = require('json-api-errors') |
| 67 | + |
| 68 | +var errors = new Errors() |
| 69 | +errors.add('CUSTOM_ERROR', 'Custom error message') |
| 70 | + |
| 71 | +var errs = errors.get() |
| 72 | +// { |
| 73 | +// errors: [{ id: 'CUSTOM_ERROR', message: 'Custom error message' }] |
| 74 | +// } |
| 75 | +``` |
| 76 | + |
| 77 | +#### `Errors.clear()` |
| 78 | + |
| 79 | +Remove all errors |
| 80 | + |
| 81 | +```javascript |
| 82 | +const Errors = require('json-api-errors') |
| 83 | + |
| 84 | +var errList, errors = new Errors() |
| 85 | + |
| 86 | +errors.add('CUSTOM_ERROR', 'Custom error message') |
| 87 | + |
| 88 | +errList = errors.get() |
| 89 | +console.log(errList.errors) // Length is 1 |
| 90 | + |
| 91 | +errors.clear() |
| 92 | +errList = errors.get() |
| 93 | + |
| 94 | +console.log(errList.errors) // Length is 0 |
| 95 | +``` |
| 96 | + |
| 97 | +### Tests |
| 98 | + |
| 99 | +`$ npm test` |
| 100 | + |
| 101 | +[package]: https://nodei.co/npm/json-api-errors |
| 102 | +[version]: https://www.npmjs.com/package/json-api-errors |
| 103 | +[build]: https://travis-ci.org/theworkflow/api-util |
| 104 | +[license]: https://raw.githubusercontent.com/theworkflow/json-api-errors/master/LICENSE |
| 105 | +[climate]: https://codeclimate.com/github/theworkflow/json-api-errors |
| 106 | +[coverage]: https://codeclimate.com/github/theworkflow/json-api-errors/coverage |
0 commit comments