Files
watchman/web/apps/console/src/alarms/errors.ts
T
unknownandCursor 47d3b85a4b feat(alarms): 完成 M06 报警中心与人工处理
落地正式异常创建/合并、三状态人工处置与控制台,关闭 M06 DONE_LOCAL(含 D40 认领文案「处理」)。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-19 15:27:06 +08:00

49 lines
1.5 KiB
TypeScript

/**
* 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, unknown>) => 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);
}