Create errors using JSON API errors format
$ npm install json-api-errors
Create a single errors object.
id
(String)- required
message
(String)- required
meta
(Object | String)- optional
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' }]
// }
Add an error to the errors array
id
(String)- required
message
(String)- required
meta
(Object | String)- optional
const Errors = require('json-api-errors')
var errors = new Errors()
errors.add('CUSTOM_ERROR', 'Custom error message')
Retreive all errors. Errors are returned in a JSON format
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' }]
// }
Remove all errors
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
$ npm test