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 code | Error class |
|---|---|
400 | BadRequestError |
401 | UnauthorizedError |
403 | ForbiddenError |
404 | NotFoundError |
405 | MethodNotAllowedError |
406 | NotAcceptableError |
422 | UnprocessableEntityError |
424 | FailedDependencyError |
| anything else | InternalServerError |
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.