Files
suaveplan/packages/api-stack/api/api-router/src/error-codes/error-codes.ts
2026-05-27 23:29:53 +01:00

41 lines
1.4 KiB
TypeScript

import { defineCodes, SuaveplanError } from "@suaveplan/error";
/**
* Stable error-code registry for `@suaveplan/api-router`.
*
* Codes follow the `ROUTER.<DOMAIN>` namespace as resolved by
* `@suaveplan/error`'s `defineCodes`. Every code in the registry MUST be
* thrown by a production path in this package — the spec-conformance test
* enumerates the registry and verifies each entry has a production throw
* site.
*/
export const ROUTER_CODES = defineCodes("ROUTER", [
"ROUTE_COLLISION",
"INVALID_PREFIX",
"MIDDLEWARE_INSERTION_DENIED",
"SLOT_FLIP_FAILED",
"SLOT_DISPOSE_TIMEOUT",
] as const);
/** Union of every code resolved through {@link ROUTER_CODES}. */
export type RouterCode = (typeof ROUTER_CODES)[keyof typeof ROUTER_CODES];
/**
* Typed error for every `@suaveplan/api-router` failure.
*
* Extends `SuaveplanError` so callers can discriminate errors from this
* package via `error instanceof RouterError` or the stable `error.code`
* property. The `context` slot carries call-site metadata
* (matched route, plugin id, slot id, etc.) without leaking internals.
*/
export class RouterError extends SuaveplanError<RouterCode> {
public constructor(
code: RouterCode,
message: string,
options?: { cause?: Error; context?: Record<string, unknown> }
) {
super(code, message, options);
this.name = "RouterError";
}
}