Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Core Concepts/Error Handling

Error Handling

Return predictable HTTP errors without exposing internal exceptions.

Complete error handler

index.ts
import { Volt, HttpError, NotFoundError } from 'voltts';

const app = new Volt({ development: false });
app.onError((error, request) => {
  if (error instanceof HttpError) return error.toResponse(false);
  console.error(request.url, error);
  return Response.json({ error: 'Internal server error' }, { status: 500 });
});
app.get('/users/:id', () => {
  throw new NotFoundError('User not found');
});
if (globalThis._importMeta_.main) app.listen(3000);
export default app;

Try a missing resource

bash
bun run index.ts
curl -i http://localhost:3000/users/42

Expect 404 with status, error, and message fields. Built-in errors include BadRequestError (400), UnauthorizedError (401), ForbiddenError (403), NotFoundError (404), PayloadTooLargeError (413), and UnsupportedMediaTypeError (415).

Preserve validation errors

A custom onError replaces the default error conversion. Check instanceof HttpError first to retain validation statuses and details. Return a generic 500 message for unexpected errors and log the original exception privately. The default development mode can expose stack traces; keep development false for public services.