ErrorHandlerBase classes
Reusable error handlers as classes with static deps, handle(), and this.inject for services.
AI generated, pending review
When an error handler needs services (a logger, metrics), extend ErrorHandlerBase and register with host.http.error(Cls). Class handlers do not take an options object; declare static deps on the class instead.
import { ErrorHandlerBase, FlareError, FlareHost, FlareResponse, Logger,} from "@flare-ts/core";import type { HttpErrorContext, ResponseLike } from "@flare-ts/core";import { node } from "@flare-ts/core/node";
class HttpErrors extends ErrorHandlerBase { public static override deps = [Logger]; readonly #log = this.inject(Logger);
override handle( err: FlareError | Error, context: HttpErrorContext, ): ResponseLike | void { this.#log.error(err, "request failed", { stage: context.stage ?? null, target: context.target ?? null, }); if (err instanceof FlareError && err.category === "conflict") { return new FlareResponse(409, { error: err.name }); } }}
const host = new FlareHost(node);host.http.error(HttpErrors);
const app = host.build();app.run();Required static members
Section titled “Required static members”| Member | Required? | Purpose |
|---|---|---|
static deps | Yes | Services via this.inject() ([] if none) |
static config | Optional | Config via this.config() |
Registration throws at call time if static deps is missing.
handle()
Section titled “handle()”Override handle(err, context) and return a ResponseLike to override the response, or return nothing to defer. The second argument is HttpErrorContext, not FlareHttpContext.
There is no static state on error handlers. They’re not part of the request-state graph.
Group registration
Section titled “Group registration”host.http.group("/api", (g) => { g.error(HttpErrors); g.get("/items", () => new FlareResponse(200, { ok: true })); return g.register();});Arc-level handlers run first, then group handlers.