@beignet/core
Version:
Core framework primitives for Beignet
36 lines (30 loc) • 951 B
text/typescript
import { isContractError } from "./client.js";
/**
* User-facing copy overrides keyed by error code.
*/
export type ErrorMessageOverrides = Partial<Record<string, string>>;
/**
* Map an unknown error to user-facing copy.
*
* Non-`ContractError` values return the fallback. Override copy per catalog
* code with `overrides`. Client-side input validation failures return a
* generic "check the highlighted fields" message; other contract errors
* return the error message.
*/
export function contractErrorMessage(
error: unknown,
fallback: string,
overrides: ErrorMessageOverrides = {},
): string {
if (!isContractError(error)) {
return fallback;
}
const override = error.code ? overrides[error.code] : undefined;
if (override) {
return override;
}
if (error.hasSource("client") && error.hasCode("INPUT_VALIDATION_ERROR")) {
return "Check the highlighted fields and try again.";
}
return error.message;
}