Skip to content

Inline error handlers

Register function error handlers with host.http.error and optional named inject maps.

AI generated, pending review Updated 10 days ago · Flare 0.3

Register function handlers on host.http.error or g.error inside a route group.

OverloadSignature
Function.error(HttpErrorHandler)
Function + options.error(HttpErrorHandlerOptions, HttpErrorHandler)
import { FlareError, FlareHost, FlareResponse } from "@flare-ts/core";
import { flareErrorCodes } from "@flare-ts/core/errors";
import { node } from "@flare-ts/core/node";
const UserErrors = flareErrorCodes({
not_found: {
UserNotFound: { expose: true, code: 1001 },
},
});
const host = new FlareHost(node);
host.http.error((err, context) => {
if (err instanceof FlareError && err.category === "not_found") {
return new FlareResponse(404, {
error: err.name,
requestId: context.requestId,
});
}
});
host.http.get("/users/missing", () => {
throw new FlareError(UserErrors.not_found.UserNotFound);
});
const app = host.build();
app.run();
(err, context, scope) => ResponseLike | void | Promise<...>
ArgumentTypeNotes
errFlareError | ErrorThe thrown value
contextHttpErrorContextrequestId, method, url, optional stage / target
scopeHandlerScopeinject map entries and scope.config

There is no ctx.state on error handlers. Pass data through FlareError.detail or an injected service.

OptionPurpose
injectNamed map { name: Token } for scope.name
nameDisplay name for the generated wrapper
import { FlareHost, FlareResponse, Logger } from "@flare-ts/core";
import { node } from "@flare-ts/core/node";
const host = new FlareHost(node);
host.http.error({ inject: { log: Logger } }, (err, context, scope) => {
scope.log.error(err, "unhandled", { requestId: context.requestId });
return new FlareResponse(500, { error: "Internal Server Error" });
});
const app = host.build();
app.run();