refactor: improve error handling in notification components

- Enhanced error messages in HandleNotifications and notificationRouter to provide more specific feedback.
- Updated email and Discord notification functions to throw detailed errors on failure.
- Ensured consistent error handling across notification utilities for better debugging.
This commit is contained in:
Mauricio Siu
2025-11-26 02:39:01 -05:00
parent 55748749fd
commit bacadccaa9
8 changed files with 1061 additions and 1034 deletions
@@ -1261,8 +1261,10 @@ export const HandleNotifications = ({ notificationId }: Props) => {
}); });
} }
toast.success("Connection Success"); toast.success("Connection Success");
} catch { } catch (error) {
toast.error("Error testing the provider"); toast.error(
`Error testing the provider ${error instanceof Error ? error.message : "Unknown error"}`,
);
} }
}} }}
> >
@@ -111,7 +111,7 @@ export const notificationRouter = createTRPCRouter({
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
message: "Error testing the notification", message: `${error instanceof Error ? error.message : "Unknown error"}`,
cause: error, cause: error,
}); });
} }
@@ -228,7 +228,7 @@ export const notificationRouter = createTRPCRouter({
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
message: "Error testing the notification", message: `${error instanceof Error ? error.message : "Unknown error"}`,
cause: error, cause: error,
}); });
} }
@@ -285,7 +285,7 @@ export const notificationRouter = createTRPCRouter({
} catch (error) { } catch (error) {
throw new TRPCError({ throw new TRPCError({
code: "BAD_REQUEST", code: "BAD_REQUEST",
message: "Error testing the notification", message: `${error instanceof Error ? error.message : "Unknown error"}`,
cause: error, cause: error,
}); });
} }
@@ -7,8 +7,8 @@ import { and, eq } from "drizzle-orm";
import { import {
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
sendLarkNotification,
sendGotifyNotification, sendGotifyNotification,
sendLarkNotification,
sendNtfyNotification, sendNtfyNotification,
sendSlackNotification, sendSlackNotification,
sendTelegramNotification, sendTelegramNotification,
@@ -52,6 +52,7 @@ export const sendBuildErrorNotifications = async ({
for (const notification of notificationList) { for (const notification of notificationList) {
const { email, discord, telegram, slack, gotify, ntfy, lark } = const { email, discord, telegram, slack, gotify, ntfy, lark } =
notification; notification;
try {
if (email) { if (email) {
const template = await renderAsync( const template = await renderAsync(
BuildFailedEmail({ BuildFailedEmail({
@@ -63,7 +64,11 @@ export const sendBuildErrorNotifications = async ({
date: date.toLocaleString(), date: date.toLocaleString(),
}), }),
).catch(); ).catch();
await sendEmailNotification(email, "Build failed for dokploy", template); await sendEmailNotification(
email,
"Build failed for dokploy",
template,
);
} }
if (discord) { if (discord) {
@@ -326,5 +331,8 @@ export const sendBuildErrorNotifications = async ({
}, },
}); });
} }
} catch (error) {
console.log(error);
}
} }
}; };
@@ -8,8 +8,8 @@ import { and, eq } from "drizzle-orm";
import { import {
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
sendLarkNotification,
sendGotifyNotification, sendGotifyNotification,
sendLarkNotification,
sendNtfyNotification, sendNtfyNotification,
sendSlackNotification, sendSlackNotification,
sendTelegramNotification, sendTelegramNotification,
@@ -55,7 +55,7 @@ export const sendBuildSuccessNotifications = async ({
for (const notification of notificationList) { for (const notification of notificationList) {
const { email, discord, telegram, slack, gotify, ntfy, lark } = const { email, discord, telegram, slack, gotify, ntfy, lark } =
notification; notification;
try {
if (email) { if (email) {
const template = await renderAsync( const template = await renderAsync(
BuildSuccessEmail({ BuildSuccessEmail({
@@ -67,7 +67,11 @@ export const sendBuildSuccessNotifications = async ({
environmentName, environmentName,
}), }),
).catch(); ).catch();
await sendEmailNotification(email, "Build success for dokploy", template); await sendEmailNotification(
email,
"Build success for dokploy",
template,
);
} }
if (discord) { if (discord) {
@@ -338,5 +342,8 @@ export const sendBuildSuccessNotifications = async ({
}, },
}); });
} }
} catch (error) {
console.log(error);
}
} }
}; };
@@ -7,8 +7,8 @@ import { and, eq } from "drizzle-orm";
import { import {
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
sendLarkNotification,
sendGotifyNotification, sendGotifyNotification,
sendLarkNotification,
sendNtfyNotification, sendNtfyNotification,
sendSlackNotification, sendSlackNotification,
sendTelegramNotification, sendTelegramNotification,
@@ -52,7 +52,7 @@ export const sendDatabaseBackupNotifications = async ({
for (const notification of notificationList) { for (const notification of notificationList) {
const { email, discord, telegram, slack, gotify, ntfy, lark } = const { email, discord, telegram, slack, gotify, ntfy, lark } =
notification; notification;
try {
if (email) { if (email) {
const template = await renderAsync( const template = await renderAsync(
DatabaseBackupEmail({ DatabaseBackupEmail({
@@ -356,5 +356,8 @@ export const sendDatabaseBackupNotifications = async ({
}, },
}); });
} }
} catch (error) {
console.log(error);
}
} }
}; };
@@ -7,8 +7,8 @@ import { and, eq } from "drizzle-orm";
import { import {
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
sendLarkNotification,
sendGotifyNotification, sendGotifyNotification,
sendLarkNotification,
sendNtfyNotification, sendNtfyNotification,
sendSlackNotification, sendSlackNotification,
sendTelegramNotification, sendTelegramNotification,
@@ -39,7 +39,7 @@ export const sendDockerCleanupNotifications = async (
for (const notification of notificationList) { for (const notification of notificationList) {
const { email, discord, telegram, slack, gotify, ntfy, lark } = const { email, discord, telegram, slack, gotify, ntfy, lark } =
notification; notification;
try {
if (email) { if (email) {
const template = await renderAsync( const template = await renderAsync(
DockerCleanupEmail({ message, date: date.toLocaleString() }), DockerCleanupEmail({ message, date: date.toLocaleString() }),
@@ -181,7 +181,7 @@ export const sendDockerCleanupNotifications = async (
elements: [ elements: [
{ {
tag: "markdown", tag: "markdown",
content: `**Status:**\nSuccessful`, content: "**Status:**\nSuccessful",
text_align: "left", text_align: "left",
text_size: "normal_v2", text_size: "normal_v2",
}, },
@@ -216,5 +216,8 @@ export const sendDockerCleanupNotifications = async (
}, },
}); });
} }
} catch (error) {
console.log(error);
}
} }
}; };
@@ -7,8 +7,8 @@ import { eq } from "drizzle-orm";
import { import {
sendDiscordNotification, sendDiscordNotification,
sendEmailNotification, sendEmailNotification,
sendLarkNotification,
sendGotifyNotification, sendGotifyNotification,
sendLarkNotification,
sendNtfyNotification, sendNtfyNotification,
sendSlackNotification, sendSlackNotification,
sendTelegramNotification, sendTelegramNotification,
@@ -34,18 +34,23 @@ export const sendDokployRestartNotifications = async () => {
const { email, discord, telegram, slack, gotify, ntfy, lark } = const { email, discord, telegram, slack, gotify, ntfy, lark } =
notification; notification;
try {
if (email) { if (email) {
const template = await renderAsync( const template = await renderAsync(
DokployRestartEmail({ date: date.toLocaleString() }), DokployRestartEmail({ date: date.toLocaleString() }),
).catch(); ).catch();
await sendEmailNotification(email, "Dokploy Server Restarted", template);
await sendEmailNotification(
email,
"Dokploy Server Restarted",
template,
);
} }
if (discord) { if (discord) {
const decorate = (decoration: string, text: string) => const decorate = (decoration: string, text: string) =>
`${discord.decoration ? decoration : ""} ${text}`.trim(); `${discord.decoration ? decoration : ""} ${text}`.trim();
try {
await sendDiscordNotification(discord, { await sendDiscordNotification(discord, {
title: decorate(">", "`✅` Dokploy Server Restarted"), title: decorate(">", "`✅` Dokploy Server Restarted"),
color: 0x57f287, color: 0x57f287,
@@ -71,27 +76,19 @@ export const sendDokployRestartNotifications = async () => {
text: "Dokploy Restart Notification", text: "Dokploy Restart Notification",
}, },
}); });
} catch (error) {
console.log(error);
}
} }
if (gotify) { if (gotify) {
const decorate = (decoration: string, text: string) => const decorate = (decoration: string, text: string) =>
`${gotify.decoration ? decoration : ""} ${text}\n`; `${gotify.decoration ? decoration : ""} ${text}\n`;
try {
await sendGotifyNotification( await sendGotifyNotification(
gotify, gotify,
decorate("✅", "Dokploy Server Restarted"), decorate("✅", "Dokploy Server Restarted"),
`${decorate("🕒", `Date: ${date.toLocaleString()}`)}`, `${decorate("🕒", `Date: ${date.toLocaleString()}`)}`,
); );
} catch (error) {
console.log(error);
}
} }
if (ntfy) { if (ntfy) {
try {
await sendNtfyNotification( await sendNtfyNotification(
ntfy, ntfy,
"Dokploy Server Restarted", "Dokploy Server Restarted",
@@ -99,25 +96,17 @@ export const sendDokployRestartNotifications = async () => {
"", "",
`🕒Date: ${date.toLocaleString()}`, `🕒Date: ${date.toLocaleString()}`,
); );
} catch (error) {
console.log(error);
}
} }
if (telegram) { if (telegram) {
try {
await sendTelegramNotification( await sendTelegramNotification(
telegram, telegram,
`<b>✅ Dokploy Server Restarted</b>\n\n<b>Date:</b> ${format(date, "PP")}\n<b>Time:</b> ${format(date, "pp")}`, `<b>✅ Dokploy Server Restarted</b>\n\n<b>Date:</b> ${format(date, "PP")}\n<b>Time:</b> ${format(date, "pp")}`,
); );
} catch (error) {
console.log(error);
}
} }
if (slack) { if (slack) {
const { channel } = slack; const { channel } = slack;
try {
await sendSlackNotification(slack, { await sendSlackNotification(slack, {
channel: channel, channel: channel,
attachments: [ attachments: [
@@ -134,13 +123,9 @@ export const sendDokployRestartNotifications = async () => {
}, },
], ],
}); });
} catch (error) {
console.log(error);
}
} }
if (lark) { if (lark) {
try {
await sendLarkNotification(lark, { await sendLarkNotification(lark, {
msg_type: "interactive", msg_type: "interactive",
card: { card: {
@@ -182,7 +167,7 @@ export const sendDokployRestartNotifications = async () => {
elements: [ elements: [
{ {
tag: "markdown", tag: "markdown",
content: `**Status:**\nSuccessful`, content: "**Status:**\nSuccessful",
text_align: "left", text_align: "left",
text_size: "normal_v2", text_size: "normal_v2",
}, },
@@ -210,9 +195,9 @@ export const sendDokployRestartNotifications = async () => {
}, },
}, },
}); });
}
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
} }
}
}; };
@@ -1,8 +1,8 @@
import type { import type {
discord, discord,
email, email,
lark,
gotify, gotify,
lark,
ntfy, ntfy,
slack, slack,
telegram, telegram,
@@ -38,6 +38,9 @@ export const sendEmailNotification = async (
}); });
} catch (err) { } catch (err) {
console.log(err); console.log(err);
throw new Error(
`Failed to send email notification ${err instanceof Error ? err.message : "Unknown error"}`,
);
} }
}; };
@@ -45,15 +48,23 @@ export const sendDiscordNotification = async (
connection: typeof discord.$inferInsert, connection: typeof discord.$inferInsert,
embed: any, embed: any,
) => { ) => {
// try { try {
await fetch(connection.webhookUrl, { const response = await fetch(connection.webhookUrl, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ embeds: [embed] }), body: JSON.stringify({ embeds: [embed] }),
}); });
// } catch (err) { if (!response.ok) {
// console.log(err); throw new Error(
// } `Failed to send discord notification ${response.statusText}`,
);
}
} catch (err) {
console.log("error", err);
throw new Error(
`Failed to send discord notification ${err instanceof Error ? err.message : "Unknown error"}`,
);
}
}; };
export const sendTelegramNotification = async ( export const sendTelegramNotification = async (
@@ -90,13 +101,21 @@ export const sendSlackNotification = async (
message: any, message: any,
) => { ) => {
try { try {
await fetch(connection.webhookUrl, { const response = await fetch(connection.webhookUrl, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(message), body: JSON.stringify(message),
}); });
if (!response.ok) {
throw new Error(
`Failed to send slack notification ${response.statusText}`,
);
}
} catch (err) { } catch (err) {
console.log(err); console.log("error", err);
throw new Error(
`Failed to send slack notification ${err instanceof Error ? err.message : "Unknown error"}`,
);
} }
}; };