Skip to content

Commit

Permalink
catch webhook errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Dec 7, 2023
1 parent a4b6b0c commit 9dd3ae5
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions src/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,43 @@ export const errorHandler =
(handler: (request: Request, env: Env) => Promise<Response>) =>
async (request: Request, env: Env): Promise<Response> => {
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) {
Expand Down

0 comments on commit 9dd3ae5

Please sign in to comment.