@everlast-brands/error-handling
v3.0.1
Published
Standardizing responses and error handling in services.
Downloads
3
Readme
Everlast Brands - Error Handling
Still Under Active Development
This package provides tools to send responses, create custom errors, and catch errors gracefully in Everlast Brands services.
All methods are compatible with ExpressJs only.
Functions
Async Wrapper: Wraps async endpoint methods
// In an express app
app.post("/user", asyncWrapper(createUser));
Send: Standardizes responses
// In a controller method in express
export async function createUser(req, res) {
const result = await User.create(req.body);
if (!result) {
send({ res, message: "Failed to create user", status: 500 });
return;
}
send({ res, data: { user: result }, message: "User Created" });
}
Classes
CustomeError: Extends the default error class and allows custom errors to be created. Utilizes an additional status property that reflects http status codes.
const FailedToCreateUserError = new CustomError(
"CreateError",
"Failed to create user",
500
);
// Usage
throw FailedToCreateUserError;