Skip to content

ErrorHandlerBase classes

Reusable error handlers as classes with static deps, handle(), and this.inject for services.

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

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();
MemberRequired?Purpose
static depsYesServices via this.inject() ([] if none)
static configOptionalConfig via this.config()

Registration throws at call time if static deps is missing.

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.

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.