From 9dd3ae5cb1cef98b6c45355fc43aa8fa6853e1a2 Mon Sep 17 00:00:00 2001 From: dromzeh <65048232+dromzeh@users.noreply.github.com> Date: Thu, 7 Dec 2023 16:45:32 +0000 Subject: [PATCH] catch webhook errors --- src/middleware/errorHandler.ts | 66 ++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/src/middleware/errorHandler.ts b/src/middleware/errorHandler.ts index ca5434c3..b8d5e2ad 100644 --- a/src/middleware/errorHandler.ts +++ b/src/middleware/errorHandler.ts @@ -4,37 +4,43 @@ export const errorHandler = (handler: (request: Request, env: Env) => Promise) => async (request: Request, env: Env): Promise => { try { - const webhookurl = env.DISCORD_LOG_WEBHOOK_URL; + try { + const webhookurl = env.DISCORD_LOG_WEBHOOK_URL; - await fetch(webhookurl, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - content: null, - embeds: [ - { - title: "API Request Success", - color: 11009917, - fields: [ - { - name: "Request URL", - value: `${request.url}`, - inline: true, - }, - { - name: "CF Colo", - value: `${request.cf.colo || "Unknown"}`, - inline: true, - }, - ], - timestamp: new Date().toISOString(), - }, - ], - attachments: [], - }), - }); + await fetch(webhookurl, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + content: null, + embeds: [ + { + title: "API Request Success", + color: 11009917, + fields: [ + { + name: "Request URL", + value: `${request.url}`, + inline: true, + }, + { + name: "CF Colo", + value: `${ + request.cf.colo || "Unknown" + }`, + inline: true, + }, + ], + timestamp: new Date().toISOString(), + }, + ], + attachments: [], + }), + }); + } catch (e) { + console.error(e); + } return await handler(request, env); } catch (error) {