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 { status } from "elysia";
|
||||||
import { TTLCache } from "enka.ts";
|
import { TTLCache } from "enka.ts";
|
||||||
|
|
||||||
|
import { createLogger } from "@/lib/logger";
|
||||||
|
|
||||||
const routesCache = new TTLCache();
|
const routesCache = new TTLCache();
|
||||||
type Pending = {
|
type Pending = {
|
||||||
data: unknown;
|
data: unknown;
|
||||||
@@ -12,6 +14,8 @@ type Pending = {
|
|||||||
|
|
||||||
const pending = new Map<string, Pending>();
|
const pending = new Map<string, Pending>();
|
||||||
|
|
||||||
|
const log = createLogger("WH");
|
||||||
|
|
||||||
export function webhookMerger(app: App) {
|
export function webhookMerger(app: App) {
|
||||||
return app.post("/webhook/:id", async ({ body, params: { id } }) => {
|
return app.post("/webhook/:id", async ({ body, params: { id } }) => {
|
||||||
const map = await getRoutes();
|
const map = await getRoutes();
|
||||||
@@ -31,13 +35,15 @@ export function webhookMerger(app: App) {
|
|||||||
|
|
||||||
existing.timer = setTimeout(async () => {
|
existing.timer = setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
await fetch(url, {
|
const res = await fetch(url, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify(existing.data),
|
body: JSON.stringify(existing.data),
|
||||||
});
|
});
|
||||||
|
if (!res.ok)
|
||||||
|
log.warn`Could not forward webhook: ${res.status}\n${await res.json().catch(() => res.text())}`;
|
||||||
} finally {
|
} finally {
|
||||||
pending.delete(id);
|
pending.delete(id);
|
||||||
}
|
}
|
||||||
@@ -79,9 +85,7 @@ async function getRoutes(): Promise<RouteMap> {
|
|||||||
|
|
||||||
if (cached) return cached as RouteMap;
|
if (cached) return cached as RouteMap;
|
||||||
|
|
||||||
const res = await fetch(
|
const res = await fetch(env.WH_ROUTE_MAP).then((r) => r.ok && r.json());
|
||||||
env.WH_ROUTE_MAP,
|
|
||||||
).then((r) => r.ok && r.json());
|
|
||||||
|
|
||||||
if (!res) throw new Error("cant get route map");
|
if (!res) throw new Error("cant get route map");
|
||||||
routesCache.set(".", res, 900);
|
routesCache.set(".", res, 900);
|
||||||
|
|||||||
Reference in New Issue
Block a user