@setu-ts/exceptions — HTTP errors and error responses
Add it: REST scaffolds install this package; there is no setu add entry or capability. In
setu.config.ts, register errorHandler on the global middleware pipeline.
Format thrown errors consistently
import { errorHandler } from "@setu-ts/exceptions";
app.middleware.add(errorHandler({ format: "rfc9457" }), {
priority: 0,
name: "error-handler",
});
Refuse a request with an HTTP error
import type { IRouterApi } from "@setu-ts/common";
import { notFound } from "@setu-ts/exceptions";
export function registerUserRoutes(router: IRouterApi): void {
router.get("/users/:id", {
handler: (ctx) => {
throw notFound(`User ${ctx.params.id} not found`);
},
});
}
Behaviour the types do not show
- Priority
0makes the handler wrap downstream middleware and routes; it catches their throws. formatdefaults to"default";"rfc9457"sendsapplication/problem+json.maskInternalErrorsdefaults totrue: unexpected 5xx messages become the status title; deliberately thrownHttpErrormessages are preserved.includeStackTracedefaults tofalse; masked errors never expose a stack even when enabled.logErrorsdefaults totrue: warn below 500, error otherwise. Without a logger, only 5xx reaches the runtime error sink.respondsees the normalized, masked error and resolved status. A returned response owns its status, headers and body;undefinedfalls through to the configured formatter.
Other options
badRequest,unauthorized,forbidden,conflict,tooManyRequestsandserviceUnavailablecreate other HTTP refusals.validationErrorstores field failures indetails.errors;internalServerErroraccepts a cause.formatalso accepts a formatter function or the deprecated"rfc7807"arm.