/** * Alarm API failures as fixed copy. The server message is never shown. * Boundary: reason codes and the numeric envelope code only. * Depends on BusinessError from the shared API client. */ import { BusinessError } from "@watchman/api"; type Translate = (key: string, named?: Record) => string; type Exists = (key: string) => boolean; export function describeAlarmError( error: unknown, t: Translate, te: Exists, fallbackKey: string, permissionNode?: string, ): string { if (!(error instanceof BusinessError)) { return t(fallbackKey); } if (error.status === 403 || error.code === 42000) { if (permissionNode) { return t("alarm.errors.forbidden", { node: permissionNode }); } return t("alarm.errors.forbiddenPlain"); } const reason = error.details.reason; if (typeof reason === "string" && te(`alarm.errors.${reason}`)) { return t(`alarm.errors.${reason}`); } if (te(`codes.${error.code}`)) { return `${t(`codes.${error.code}`)} (${error.code})`; } return `${t(fallbackKey)} (${error.code})`; } export function isAlarmNotFound(error: unknown): boolean { return error instanceof BusinessError && error.status === 404; } export function isAlarmConflict(error: unknown): boolean { return error instanceof BusinessError && error.status === 409; } export function isAlarmForbidden(error: unknown): boolean { return error instanceof BusinessError && (error.status === 403 || error.code === 42000); }