fix: add webhook forward error logging

This commit is contained in:
2026-06-11 19:01:15 +07:00 Unverified
parent dcd521ee3c
commit 88def78ce6
+8 -4
View File
@@ -3,6 +3,8 @@ import { env } from "bun";
import { status } from "elysia";
import { TTLCache } from "enka.ts";
import { createLogger } from "@/lib/logger";
const routesCache = new TTLCache();
type Pending = {
data: unknown;
@@ -12,6 +14,8 @@ type Pending = {
const pending = new Map<string, Pending>();
const log = createLogger("WH");
export function webhookMerger(app: App) {
return app.post("/webhook/:id", async ({ body, params: { id } }) => {
const map = await getRoutes();
@@ -31,13 +35,15 @@ export function webhookMerger(app: App) {
existing.timer = setTimeout(async () => {
try {
await fetch(url, {
const res = await fetch(url, {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(existing.data),
});
if (!res.ok)
log.warn`Could not forward webhook: ${res.status}\n${await res.json().catch(() => res.text())}`;
} finally {
pending.delete(id);
}
@@ -79,9 +85,7 @@ async function getRoutes(): Promise<RouteMap> {
if (cached) return cached as RouteMap;
const res = await fetch(
env.WH_ROUTE_MAP,
).then((r) => r.ok && r.json());
const res = await fetch(env.WH_ROUTE_MAP).then((r) => r.ok && r.json());
if (!res) throw new Error("cant get route map");
routesCache.set(".", res, 900);