fix: add webhook forward error logging
This commit is contained in:
+8
-4
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user