Files
suaveplan/packages/foundation/data/react-packer/src/error-codes/error-codes.ts
2026-05-27 23:47:53 +01:00

52 lines
1.5 KiB
TypeScript

import {
defineCodes,
SuaveplanError,
type SuaveplanErrorOptions,
} from "@suaveplan/error";
/**
* Stable error code registry for `@suaveplan/react-packer`.
*
* Every failure raised by the React-tier packer surface (hook lifecycle,
* `@font-face` injection, pre-aborted signals) maps to one of these codes.
* The underlying error from `@suaveplan/packer-browser` or
* `document.fonts.load` is preserved on `.cause` so consumers can walk
* the chain.
*/
export const REACT_PACKER_CODES = defineCodes("REACT_PACKER", [
"LOAD_FAILED",
"FONT_LOAD_FAILED",
"ABORTED",
] as const);
export type ReactPackerErrorCode =
(typeof REACT_PACKER_CODES)[keyof typeof REACT_PACKER_CODES];
const BRAND: unique symbol = Symbol("@suaveplan/react-packer.error");
/**
* Concrete `SuaveplanError` subclass for the React-tier packer package.
*
* Carries a brand symbol so cross-realm `instanceof` failures still permit
* defence-in-depth via `ReactPackerError.is(value)`.
*/
export class ReactPackerError extends SuaveplanError<ReactPackerErrorCode> {
public readonly [BRAND]: true;
public constructor(
code: ReactPackerErrorCode,
messageKey: string,
options?: SuaveplanErrorOptions
) {
super(code, messageKey, options);
this[BRAND] = true;
}
public static is(value: unknown): value is ReactPackerError {
return (
value instanceof ReactPackerError &&
(value as ReactPackerError)[BRAND] === true
);
}
}