sendable-error v0.3.2
sendable-error ·
Composable errors to simplify creating useful failure responses for APIs
npm i sendable-error
Note: this package is in early development, use with caution
SendableErrors
provide built-in support for:
- An easy to use builder interface to construct errors
- A unified way to send your errors as a JSON response
- Error codes to easily identify error types on the client side
- Public and private messages & details so your APIs don't leak technical information yet retaining verbose logging
- Trace IDs allow you to identify specific errors and allows user's to point you in the right direction when they encounter a bug
- A customizable logger interface
Getting Started
Creating a new error from scratch:
import {SendableError, ErrorCode} from "sendable-error";
const CODE_MISSING_REQUIRED = new ErrorCode("validation/missing-required", "Missing required field", {statusCode: 400});
export const updateUser = (req, res) => {
if (!req.body.id) {
return SendableError.of(CODE_MISSING_REQUIRED)
.message("Missing required field 'id'")
.details({
field: "id"
})
.build()
.send(res);
}
};
In this example if you miss the ID field from the body you shall receive a 400 status code error with the body:
{
"code": "validation/missing-required",
"message": "Missing required field 'id'",
"traceId": "8ab9c56a-90d1-5e71-b67a-d6b725837802",
"details": {
"field": "id"
}
}
22 days ago
1 month ago
2 months ago
2 months ago
2 months ago
6 months ago
6 months ago
10 months ago
8 months ago
10 months ago
10 months ago
8 months ago
10 months ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago