elysia-sentry-errors
v0.1.0
Published
Typed HTTP exceptions and safe Sentry-aware error handling for Elysia
Maintainers
Readme
@codearcade/elysia-sentry-errors
Typed HTTP exceptions and secure, Sentry-aware error responses for Elysia.
This package is ESM-only and must be loaded with import.
Install
bun add @codearcade/elysia-sentry-errors @sentry/elysia elysiaInitialize Sentry once in your application, wrap the app with Sentry's Elysia integration, and register this plugin:
import * as Sentry from "@sentry/elysia";
import { Elysia } from "elysia";
import {
NotFoundException,
httpExceptionPlugin,
} from "@codearcade/elysia-sentry-errors";
Sentry.init({ dsn: process.env.SENTRY_DSN });
const app = Sentry.withElysia(new Elysia())
.use(httpExceptionPlugin())
.get("/users/:id", ({ params }) => {
throw new NotFoundException(`User ${params.id} was not found`);
});Responses have a predictable shape:
{
"statusCode": 404,
"code": "NOT_FOUND",
"message": "User 123 was not found"
}Unexpected errors are captured and returned as a safe generic 500 response.
Intentional HttpException instances are captured only for 5xx status codes.
Options
httpExceptionPlugin({
captureErrors: true,
exposeUnexpectedErrorMessages: false,
unexpectedErrorMessage: "Internal Server Error",
shouldCaptureHttpException: (error) => error.statusCode >= 500,
captureException: Sentry.captureException,
});Passing an object to an exception returns that object as the response body. This supports custom API response formats while the exception's HTTP status remains authoritative.
throw new BadRequestException({
statusCode: 400,
code: "INVALID_EMAIL",
message: "Email is invalid",
});