Skip to main content

wrapException

import { wrapException } from '@opra/http';
wrapException(error: any): OpraHttpError

Maps any error to an OPRA HTTP error class based on its HTTP status code. If error is already an OpraHttpError it is returned unchanged.

Status detection reads error.status (number) or calls error.getStatus() if present. All other errors default to InternalServerError.


Status mapping

Status codeError class
400BadRequestError
401UnauthorizedError
403ForbiddenError
404NotFoundError
405MethodNotAllowedError
406NotAcceptableError
422UnprocessableEntityError
424FailedDependencyError
anything elseInternalServerError

Usage

import { wrapException } from '@opra/http';
import { NotFoundError } from '@opra/common';

try {
// ... some operation
} catch (err) {
const opraErr = wrapException(err);
// opraErr is now a typed OpraHttpError with a .status property
console.log(opraErr.status, opraErr.message);
}

wrapException is used internally by HttpHandler to normalize unhandled exceptions before sending error responses.