|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { bot } from "#bot"; |
| 3 | + |
| 4 | +import { formatErrorDetails } from "../../error-details.ts"; |
| 5 | +import { escapeHtml } from "../../escape-html.ts"; |
| 6 | +import { sendReport } from "../../telegram/report.ts"; |
| 7 | + |
| 8 | +const reportedWebhookError = Symbol("reportedWebhookError"); |
| 9 | + |
| 10 | +interface WebhookReportContext { |
| 11 | + eventId?: string; |
| 12 | + eventName?: string; |
| 13 | + source?: string; |
| 14 | +} |
| 15 | + |
| 16 | +function isObject(value: unknown): value is Record<PropertyKey, unknown> { |
| 17 | + return typeof value === "object" && value !== null; |
| 18 | +} |
| 19 | + |
| 20 | +function markWebhookErrorReported(error: unknown) { |
| 21 | + if (!isObject(error)) return; |
| 22 | + |
| 23 | + error[reportedWebhookError] = true; |
| 24 | +} |
| 25 | + |
| 26 | +function hasReportedWebhookError(error: unknown) { |
| 27 | + return isObject(error) && error[reportedWebhookError] === true; |
| 28 | +} |
| 29 | + |
| 30 | +function getEventName(event: unknown) { |
| 31 | + if (!isObject(event)) return undefined; |
| 32 | + |
| 33 | + const name = typeof event.name === "string" ? event.name : undefined; |
| 34 | + const payload = isObject(event.payload) ? event.payload : undefined; |
| 35 | + const action = typeof payload?.action === "string" ? payload.action : undefined; |
| 36 | + |
| 37 | + if (!name) return undefined; |
| 38 | + if (!action || name.includes(".")) return name; |
| 39 | + |
| 40 | + return `${name}.${action}`; |
| 41 | +} |
| 42 | + |
| 43 | +function getEventInfo(error: unknown): WebhookReportContext { |
| 44 | + if (!isObject(error) || !isObject(error.event)) return {}; |
| 45 | + |
| 46 | + return { |
| 47 | + eventId: typeof error.event.id === "string" ? error.event.id : undefined, |
| 48 | + eventName: getEventName(error.event), |
| 49 | + }; |
| 50 | +} |
| 51 | + |
| 52 | +export function buildWebhookErrorReport(error: unknown, context: WebhookReportContext = {}) { |
| 53 | + const event = getEventInfo(error); |
| 54 | + const eventName = context.eventName ?? event.eventName; |
| 55 | + const eventId = context.eventId ?? event.eventId; |
| 56 | + const lines = ["<b>GitHub Webhook Error:</b>"]; |
| 57 | + |
| 58 | + if (eventName) lines.push(`Event: <code>${escapeHtml(eventName)}</code>`); |
| 59 | + if (eventId) lines.push(`Delivery: <code>${escapeHtml(eventId)}</code>`); |
| 60 | + if (context.source) lines.push(`Source: <code>${escapeHtml(context.source)}</code>`); |
| 61 | + |
| 62 | + lines.push("", "<b>Message:</b>", "", `<pre>${escapeHtml(formatErrorDetails(error))}</pre>`, "", "#webhook #error"); |
| 63 | + |
| 64 | + return lines.join("\n"); |
| 65 | +} |
| 66 | + |
| 67 | +export async function reportWebhookError(error: unknown, context: WebhookReportContext = {}) { |
| 68 | + if (hasReportedWebhookError(error)) return; |
| 69 | + |
| 70 | + markWebhookErrorReported(error); |
| 71 | + console.error(error); |
| 72 | + |
| 73 | + return sendReport(bot.api, buildWebhookErrorReport(error, context)); |
| 74 | +} |
0 commit comments