$ npm install @feathersjs/errors --save
The @feathersjs/errors
module contains a set of standard error classes used by all other Feathers modules as well as an Express error handler to format those - and other - errors and setting the correct HTTP status codes for REST calls.
The following error types, all of which are instances of FeathersError
are available:
ProTip: All of the Feathers plugins will automatically emit the appropriate Feathers errors for you. For example, most of the database adapters will already send
Conflict
orUnprocessable
errors with the validation errors from the ORM.
- 400:
BadRequest
- 401:
NotAuthenticated
- 402:
PaymentError
- 403:
Forbidden
- 404:
NotFound
- 405:
MethodNotAllowed
- 406:
NotAcceptable
- 408:
Timeout
- 409:
Conflict
- 411:
LengthRequired
- 422:
Unprocessable
- 429:
TooManyRequests
- 500:
GeneralError
- 501:
NotImplemented
- 502:
BadGateway
- 503:
Unavailable
Feathers errors contain the following fields:
name
- The error name (ie. "BadRequest", "ValidationError", etc.)message
- The error message stringcode
- The HTTP status codeclassName
- A CSS class name that can be handy for styling errors based on the error type. (ie. "bad-request" , etc.)data
- An object containing anything you passed to a Feathers error except for theerrors
object.errors
- An object containing whatever was passed to a Feathers error insideerrors
. This is typically validation errors or if you want to group multiple errors together.
ProTip: To convert a Feathers error back to an object call
error.toJSON()
. A normalconsole.log
of a JavaScript Error object will not automatically show those additional properties described above (even though they can be accessed directly).
You can create custom errors by extending from the FeathersError
class and calling its constructor with (msg, name, code, className, data)
:
message
- The error messagename
- The error name (e.g.my-errror
)code
- An HTTP error codeclassName
- The full name of the error classdata
- Additional data to inclue in the error
const { FeathersError } = require('@feathersjs/errors');
class UnsupportedMediaType extends FeathersError {
constructor(message, data) {
super(message, 'unsupported-media-type', 415, 'UnsupportedMediaType', data);
}
}
const error = new UnsupportedMediaType('Not supported');
console.log(error.toJSON());
Here are a few ways that you can use them:
const errors = require('@feathersjs/errors');
// If you were to create an error yourself.
const notFound = new errors.NotFound('User does not exist');
// You can wrap existing errors
const existing = new errors.GeneralError(new Error('I exist'));
// You can also pass additional data
const data = new errors.BadRequest('Invalid email', {
email: '[email protected]'
});
// You can also pass additional data without a message
const dataWithoutMessage = new errors.BadRequest({
email: '[email protected]'
});
// If you need to pass multiple errors
const validationErrors = new errors.BadRequest('Invalid Parameters', {
errors: { email: 'Email already taken' }
});
// You can also omit the error message and we'll put in a default one for you
const validationErrors = new errors.BadRequest({
errors: {
email: 'Invalid Email'
}
});
Promises swallow errors if you forget to add a catch()
statement. Therefore, you should make sure that you always call .catch()
on your promises. To catch uncaught errors at a global level you can add the code below to your top-most file.
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise ', p, ' reason: ', reason);
});