
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@feathersjs/errors
Advanced tools
@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.
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' });
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 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 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.
Common error types for feathers apps
npm install @feathersjs/errors --save
Quick usage:
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: 'sergey@google.com'
});
// You can also pass additional data without a message
const dataWithoutMessage = new errors.BadRequest({
email: 'sergey@google.com'
});
// 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'
}
});
Please refer to the @feathersjs/errors API documentation for more details.
Copyright (c) 2018 Feathers Contributors
Licensed under the MIT license.
FAQs
Common error types for Feathers apps
The npm package @feathersjs/errors receives a total of 129,471 weekly downloads. As such, @feathersjs/errors popularity was classified as popular.
We found that @feathersjs/errors demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.