Skip to main content

Error Handling

OPRA maps thrown errors to structured HTTP responses automatically. Import the built-in error classes from @opra/common and throw them from any handler — no manual status code wiring needed.

import { BadRequestError, NotFoundError } from '@opra/common';

@HttpOperation.GET(':id')
.PathParam('id', { type: 'integer' })
.Response(Customer)
async get(ctx: HttpContext) {
const customer = await this.service.findById(ctx.pathParams.id);
if (!customer) throw new NotFoundError(`Customer ${ctx.pathParams.id} not found`);
return customer;
}

All error responses follow the same envelope format regardless of transport.