deno.land / x / abc@v1.3.3 / docs / exception_filter.md
Abc comes with a built-in exceptions layer that handles all unhandled
exceptions in the application and then automatically sends an appropriate
user-friendly response.
const app = new Application();
app.post("/admin", (c) => {
throw new HttpException("Forbidden", Status.Forbidden);
});When the client calls this endpoint, the response looks like this:
{
"statusCode": 403,
"message": "Forbidden"
}You can also customize the content of the response body.
const app = new Application();
app.post("/admin", (c) => {
throw new HttpException(
{
status: Status.Forbidden,
error: "This is a custom message",
},
Status.Forbidden,
);
});Using the above, this is how the response would look:
{
"status": 403,
"error": "This is a custom message"
}Once you inherit HttpException, Abc will recognize your exception and
automatically take care of the error response.
export class ForbiddenException extends HttpException {
constructor() {
super("Forbidden", Status.Forbidden);
}
}
const app = new Application();
app.post("/admin", (c) => {
throw new ForbiddenException();
});Response:
{
"statusCode": 403,
"message": "Forbidden"
}Abc has a set of exceptions inherited from HttpException:
Version Info