https://github.com/theworkflow/json-api-errors
JSON API Errors formatter
https://github.com/theworkflow/json-api-errors
Last synced: over 1 year ago
JSON representation
JSON API Errors formatter
- Host: GitHub
- URL: https://github.com/theworkflow/json-api-errors
- Owner: theworkflow
- License: mit
- Created: 2016-09-07T02:01:05.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-09-12T00:51:24.000Z (almost 10 years ago)
- Last Synced: 2025-03-22T00:01:32.901Z (over 1 year ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON API Errors
Create errors using [JSON API errors format](http://jsonapi.org/format/#errors)
[][package]
[][version]
[][build]
[][license]
[][climate]
[][coverage]
### Installation
`$ npm install json-api-errors`
### Usage
#### `Errors.createSingle(id, message, [meta])`
Create a single errors object.
- `id` (String)
- required
- `message` (String)
- required
- `meta` (Object | String)
- optional
```javascript
const Errors = require('json-api-errors')
var errors = new Errors()
var err = errors.createSingle('CUSTOM_ERROR', 'Custom error message')
console.log(err)
// {
// errors: [{ id: 'CUSTOM_ERROR', message: 'Custom error message' }]
// }
```
#### `Errors.add(id, message, [meta])`
Add an error to the errors array
- `id` (String)
- required
- `message` (String)
- required
- `meta` (Object | String)
- optional
```javascript
const Errors = require('json-api-errors')
var errors = new Errors()
errors.add('CUSTOM_ERROR', 'Custom error message')
```
#### `Errors.get()`
Retreive all errors. Errors are returned in a JSON format
```javascript
const Errors = require('json-api-errors')
var errors = new Errors()
errors.add('CUSTOM_ERROR', 'Custom error message')
var errs = errors.get()
// {
// errors: [{ id: 'CUSTOM_ERROR', message: 'Custom error message' }]
// }
```
#### `Errors.clear()`
Remove all errors
```javascript
const Errors = require('json-api-errors')
var errList, errors = new Errors()
errors.add('CUSTOM_ERROR', 'Custom error message')
errList = errors.get()
console.log(errList.errors) // Length is 1
errors.clear()
errList = errors.get()
console.log(errList.errors) // Length is 0
```
### Tests
`$ npm test`
[package]: https://nodei.co/npm/json-api-errors
[version]: https://www.npmjs.com/package/json-api-errors
[build]: https://travis-ci.org/theworkflow/api-util
[license]: https://raw.githubusercontent.com/theworkflow/json-api-errors/master/LICENSE
[climate]: https://codeclimate.com/github/theworkflow/json-api-errors
[coverage]: https://codeclimate.com/github/theworkflow/json-api-errors/coverage