// PEP-154: an exception inside Peppa is not the project owner's business. A clear refusal // (PeppaError: «подписка не выбрана») is still told to the owner; anything else goes to the // error tracker and, once per error a day, to the superadmins. import { m as tr } from "@peppa/shared/i18n"; import type { Db } from "@peppa/db"; import { createHash } from "node:crypto"; import { PeppaError } from "./errors.ts"; import { reportPlatformError } from "./error-report.ts"; import { normalizeMessage } from "./message-state.ts"; import { alertAdmins } from "./monitor.ts"; export interface FailureContext { /** Which process: "watchdog", "hook", "bot", "web". */ service: string; /** Which part failed: "dispatch PAY-7", "restart #12". */ where: string; log?: (m: string) => void; now?: Date; } /** * What to tell the project owner about a failure: the text of a clear refusal, or null — it is * a platform error (reported to the error tracker and to the superadmins, one message per error * a day) and the owner gets nothing. Never throws. */ export async function platformFailure(db: Db, e: unknown, ctx: FailureContext): Promise { if (e instanceof PeppaError) return e.message; const message = (e as Error)?.message ?? String(e); try { await reportPlatformError(db, e, { service: ctx.service, where: ctx.where }); // The same error of the same part (numbers aside) is one message a day. const fp = createHash("sha1").update(`${ctx.service}|${normalizeMessage(ctx.where)}|${normalizeMessage(message)}`).digest("hex").slice(0, 12); const day = (ctx.now ?? new Date()).toISOString().slice(0, 10); // glossary-ignore: read by superadmins only const text = tr("⚠️ Сбой в FORGE ({where}): {error}", { where: ctx.where, error: message.length > 300 ? `${message.slice(0, 300)}…` : message }); await alertAdmins(db, `fail:${fp}:${day}`, text, ctx.log ?? (() => {})); } catch {} return null; }