improve email error handling

This commit is contained in:
Alex Holliday committed 2026-09-23 12:13:27 -07:00
1 parent 41c7cfbdaa
commit 4a66cf6e11
9 files changed
+183 -33

No files matched your search

@@ -4,6 +4,7 @@ import { createMockLogger } from "../../../helpers/createMockLogger.ts";
import { makeNotification, makeMessage } from "../../../helpers/notificationMessage.ts";
import { testNotificationProviderContract } from "../../../helpers/notificationProviderContract.ts";
import type { IEmailService } from "../../../../src/service/emailService.ts";
import { AppError } from "../../../../src/utils/AppError.ts";
// ── Helpers ──────────────────────────────────────────────────────────────────
@@ -50,11 +51,29 @@ describe("EmailProvider", () => {
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "Failed to build test email content" }));
});
it("returns false when sendEmail rejects", async () => {
it("returns false and logs the cause when sendEmail rejects with a plain error", async () => {
const { provider, emailService, logger } = createProvider();
(emailService.sendEmail as jest.Mock).mockRejectedValue(new Error("SMTP auth failed"));
expect(await provider.sendTestAlert(makeNotification())).toBe(false);
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "Email test alert failed" }));
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "Email test alert failed: SMTP auth failed" }));
});
it("rethrows an AppError from sendEmail so the caller can report the reason", async () => {
const { provider, emailService, logger } = createProvider();
const error = new AppError({
message: "Email is not configured. Set the system email host in settings.",
status: 400,
details: { cause: "no host" },
});
(emailService.sendEmail as jest.Mock).mockRejectedValue(error);
await expect(provider.sendTestAlert(makeNotification())).rejects.toBe(error);
expect(logger.warn).toHaveBeenCalledWith(
expect.objectContaining({
message: "Email test alert failed: Email is not configured. Set the system email host in settings.",
details: { cause: "no host" },
})
);
});
});
@@ -84,11 +103,29 @@ describe("EmailProvider", () => {
// NotificationsService fans providers out through Promise.all, so a failed email must
// resolve to false rather than reject and take the other channels down with it.
it("returns false when sendEmail rejects", async () => {
it("returns false and logs the cause when sendEmail rejects", async () => {
const { provider, emailService, logger } = createProvider();
(emailService.sendEmail as jest.Mock).mockRejectedValue(new Error("SMTP auth failed"));
expect(await provider.sendMessage(makeNotification() as any, makeMessage())).toBe(false);
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "Email notification failed" }));
expect(logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "Email notification failed: SMTP auth failed" }));
});
it("returns false rather than throwing when sendEmail rejects with an AppError", async () => {
const { provider, emailService, logger } = createProvider();
(emailService.sendEmail as jest.Mock).mockRejectedValue(
new AppError({
message: "SMTP verification failed for smtp.example.com:587: 535 Username and Password not accepted",
status: 502,
details: { cause: "ECONNREFUSED" },
})
);
expect(await provider.sendMessage(makeNotification() as any, makeMessage())).toBe(false);
expect(logger.warn).toHaveBeenCalledWith(
expect.objectContaining({
message: "Email notification failed: SMTP verification failed for smtp.example.com:587: 535 Username and Password not accepted",
details: { cause: "ECONNREFUSED" },
})
);
});
it("builds correct subject for monitor_down", async () => {