What is @feathersjs/errors?
@feathersjs/errors is a package that provides a set of common error classes and utilities for handling errors in FeathersJS applications. It helps in creating consistent error responses and simplifies error handling in your application.
What are @feathersjs/errors's main functionalities?
Creating Custom Errors
This feature allows you to create and throw custom errors like NotFound, BadRequest, etc., which are predefined in the package. This helps in maintaining consistency in error handling across your application.
const { NotFound } = require('@feathersjs/errors');
// Throw a NotFound error
throw new NotFound('User not found');
Error Handling Middleware
This feature provides middleware for handling errors in an Express application. It checks if the error is an instance of GeneralError and sends a structured JSON response with error details.
const { GeneralError } = require('@feathersjs/errors');
// Express error handling middleware
app.use((err, req, res, next) => {
if (err instanceof GeneralError) {
res.status(err.code).json({
name: err.name,
message: err.message,
code: err.code,
className: err.className,
data: err.data,
errors: err.errors
});
} else {
next(err);
}
});
Custom Error Classes
This feature allows you to create your own custom error classes by extending the FeathersError class. This is useful for defining application-specific errors with custom properties.
const { FeathersError } = require('@feathersjs/errors');
class CustomError extends FeathersError {
constructor(message, data) {
super(message, 'custom-error', 400, 'CustomError', data);
}
}
// Throw a CustomError
throw new CustomError('This is a custom error', { additional: 'data' });
Other packages similar to @feathersjs/errors
http-errors
The http-errors package provides a set of HTTP error classes for use in Node.js applications. It is similar to @feathersjs/errors in that it helps create and manage HTTP errors, but it is more general-purpose and not specifically tied to FeathersJS.
boom
Boom is a package for creating HTTP-friendly error objects in Hapi.js applications. It provides a similar set of features for error handling and response formatting, but it is designed to work seamlessly with Hapi.js rather than FeathersJS.
express-error-handler
Express-error-handler is a middleware for handling errors in Express applications. It provides a way to define custom error handlers and format error responses, similar to the error handling middleware provided by @feathersjs/errors.
@feathersjs/errors
![Greenkeeper badge](https://badges.greenkeeper.io/feathersjs/errors.svg)
![Download Status](https://img.shields.io/npm/dm/@feathersjs/errors.svg?style=flat-square)
Common error types for feathers apps
Getting Started
Feathers errors come with feathers by default. So typically you don't need to install it at all.
In the event that you do need to install it:
npm install --save @feathersjs/errors
Documentation
Current Error Types:
BadRequest
: 400NotAuthenticated
: 401PaymentError
: 402Forbidden
: 403NotFound
: 404MethodNotAllowed
: 405NotAcceptable
: 406Timeout
: 408Conflict
: 409LengthRequired
: 411Unprocessable
: 422TooManyRequests
: 429GeneralError
: 500NotImplemented
: 501BadGateway
: 502Unavailable
: 503
Pro Tip: Feathers service adapters (ie. mongodb, memory, etc.) already emit the appropriate errors for you. :-)
Usage:
Error objects:
import errors from '@feathersjs/errors';
var notFound = new errors.NotFound('User does not exist');
var existing = new errors.GeneralError(new Error('I exist'));
var data = new errors.BadRequest('Invalid email', {email: 'sergey@google.com'});
var dataWithoutMessage = new errors.BadRequest({email: 'sergey@google.com'});
var validationErrors = new errors.BadRequest('Invalid Parameters', {errors: {email: 'Email already taken'} });
var validationErrors = new errors.BadRequest({errors: {email: 'Invalid Email'} });
Error handler:
This plugin will handle all error responses for you automatically. In the case of HTML errors, basic error pages are provided without needing to do anything. In the case of JSON, the error is normalized and serialized as JSON so that your client code will receive a predictable error object every time!
However, you can customize how errors are sent to the user on a per error code basis. This allows you to easily customize the user experience for different types of errors. If a particular configuration is omitted, it falls back to the default behavior described above.
import feathers from 'feathers';
import errorHandler from 'feathers-errors/handler';
const app = feathers();
app.configure(errorHandler({
html: {
404: 'path/to/custom-404.html',
406: (err, req, res, next) => {
res.send(...);
},
default: 'path/to/generic/error-page.html'
},
json: {
404: (err, req, res, next) => {
if (process.env.NODE_ENV === 'production') {
delete err.stack;
}
res.json({ message: 'Not found' });
},
default: (err, req, res, next) => {
res.json({ message: 'Oh no! Something went wrong' });
}
}
});
License
Copyright (c) 2017 Feathers Contributors
Licensed under the MIT license.