Skip to main content

HTTP Errors

Package: @opra/common

OPRA provides a hierarchy of typed HTTP error classes. Throw any of these from an operation handler or interceptor — adapters catch them and serialize the response automatically.


Class hierarchy

Error
└── OpraException
└── OpraHttpError
├── BadRequestError (400)
├── UnauthorizedError (401)
├── ForbiddenError (403)
│ └── PermissionError (403)
├── NotFoundError (404)
├── MethodNotAllowedError (405)
├── NotAcceptableError (406)
├── ConflictError (409)
│ └── ResourceConflictError (409)
├── UnprocessableEntityError (422)
│ └── ResourceNotAvailableError (422)
├── FailedDependencyError (424)
└── InternalServerError (500)

OpraException

Base class for all OPRA exceptions.

import { OpraException } from '@opra/common';

throw new OpraException('Something went wrong');
throw new OpraException({ message: 'Invalid input', code: 'INVALID_INPUT' });
throw new OpraException('Upstream failed', causeError);

Properties

PropertyTypeDescription
messagestringHuman-readable error message.
severity'error' | 'warning' | 'fatal'Defaults to 'error'.
codestring | undefinedMachine-readable error code.
systemstring | undefinedOriginating subsystem identifier.
detailsanyArbitrary additional details.
causeError | undefinedThe underlying cause, if any.

Constructor

new OpraException(issue: string | Partial<ErrorIssue> | Error, cause?: Error)

OpraHttpError

Extends OpraException with an HTTP status code. All HTTP-specific errors inherit from this class.

import { OpraHttpError } from '@opra/common';

throw new OpraHttpError('Service unavailable', 503);
throw new OpraHttpError({ message: 'Rate limit exceeded', code: 'RATE_LIMIT' }, 429);

Additional properties

PropertyTypeDescription
statusnumberHTTP status code.

Methods

MethodDescription
setStatus(code)Overrides the status code. Returns this for chaining.

HTTP error classes

Each error class sets a fixed status and a default message and code. All accept an optional string | Partial<ErrorIssue> argument to customize the message or code, and an optional cause Error.

throw new NotFoundError();
throw new NotFoundError('Customer not found');
throw new NotFoundError({ message: 'Customer not found', code: 'CUSTOMER_NOT_FOUND' });
ClassStatusDefault codeDefault message
BadRequestError400BAD_REQUEST'Bad request'
UnauthorizedError401UNAUTHORIZED"You don't have permission to perform this action"
ForbiddenError403FORBIDDEN'You are not authorized to perform this action'
PermissionError403PERMISSION_ERROR'You dont have permission for this operation'
NotFoundError404NOT_FOUND'Not found'
MethodNotAllowedError405METHOD_NOT_ALLOWED'Method not allowed'
NotAcceptableError406NOT_ACCEPTABLE'Not Acceptable'
ConflictError409CONFLICT'Conflict'
UnprocessableEntityError422UNPROCESSABLE_ENTITY'Unprocessable entity'
FailedDependencyError424FAILED_DEPENDENCY'The request failed due to failure of a previous request'
InternalServerError500INTERNAL_SERVER_ERROR'Internal server error'

Specialized errors

ResourceConflictError

Status 409. Use when a resource already exists with the same unique field values.

import { ResourceConflictError } from '@opra/common';

throw new ResourceConflictError('Customer', ['email']);
throw new ResourceConflictError('Customer', ['email', 'tenantId'], cause);
new ResourceConflictError(resource: string, fields: string | string[], cause?: Error)
ParameterTypeDescription
resourcestringName of the conflicting resource.
fieldsstring | string[]Field name(s) that caused the conflict.
causeErrorOptional underlying error.

ResourceNotAvailableError

Status 422. Use when a referenced resource exists in the system but is not accessible in the current context.

import { ResourceNotAvailableError } from '@opra/common';

throw new ResourceNotAvailableError('Order', orderId);
throw new ResourceNotAvailableError('Product', productId, cause);
new ResourceNotAvailableError(resource: string, keyValue?: any, cause?: Error)

ErrorIssue interface

The ErrorIssue interface describes the shape of an error's serialized form and is accepted as the constructor argument for all error classes.

interface ErrorIssue {
message: string;
severity: 'error' | 'warning' | 'fatal';
system?: string;
code?: string;
details?: any;
diagnostics?: string | string[];
stack?: string;
}

toJSON() on any OpraException returns this shape (stack is included only in development/test environments).