Creates http error which can be catched and converted to error responses.
- node: 22
Through NPM as @chubbyts/chubbyts-http-error.
npm i @chubbyts/chubbyts-http-error@^3.5.0import { createNotFound } from '@chubbyts/chubbyts-http-error/dist/http-error';
try {
throw createNotFound({ detail: 'Something went wrong', instance: 'server-1', otherKey: 'otherValue' });
} catch (e) {
console.log(e);
}detail,instance: problem details (RFC 9457)headers: the headers the error middleware should set on the error response (e.g.www-authenticate,allow,retry-after), never part of the body- any other key: additional problem details, keys with an
undefinedvalue get skipped, reserved keys (type,status,title, ...) get ignored
import { isHttpError } from '@chubbyts/chubbyts-http-error/dist/http-error';
const errorMiddleware: Middleware = async (request, handler) => {
try {
return await handler(request);
} catch (e) {
if (isHttpError(e)) {
const { headers, _httpError, ...body } = e;
return new Response(JSON.stringify(body), {
status: e.status,
headers: { 'content-type': 'application/problem+json', ...headers },
});
}
throw e;
}
};2026 Dominik Zogg