-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from giselles-ai/notify-workflow-failure-trig…
…gered-by-webhook Notify workflow failure with Email when the workflow is triggered by webhooks
- Loading branch information
Showing
11 changed files
with
2,691 additions
and
2,569 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Base error class for all email-related errors | ||
*/ | ||
export class EmailError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = this.constructor.name; | ||
// Maintains proper stack trace for where error was thrown | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
/** | ||
* Error thrown when email configuration is invalid or missing | ||
*/ | ||
export class EmailConfigurationError extends EmailError { | ||
constructor(message: string) { | ||
super(message); | ||
Object.setPrototypeOf(this, EmailConfigurationError.prototype); | ||
} | ||
} | ||
|
||
/** | ||
* Error thrown when sending an email fails | ||
*/ | ||
export class EmailSendError extends EmailError { | ||
constructor( | ||
message: string, | ||
public readonly cause?: Error, | ||
) { | ||
super(message); | ||
Object.setPrototypeOf(this, EmailSendError.prototype); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { EmailConfigurationError, EmailSendError } from "./errors"; | ||
export { sendEmail } from "./send-email"; | ||
export type { EmailRecipient } from "./types"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import nodemailer from "nodemailer"; | ||
import invariant from "tiny-invariant"; | ||
import { EmailConfigurationError, EmailSendError } from "./errors"; | ||
import type { EmailRecipient } from "./types"; | ||
|
||
function createTransport() { | ||
try { | ||
invariant(process.env.SMTP_HOST, "SMTP_HOST is not set"); | ||
invariant(process.env.SMTP_PORT, "SMTP_PORT is not set"); | ||
invariant(process.env.SMTP_SECURE, "SMTP_SECURE is not set"); | ||
invariant(process.env.SMTP_USER, "SMTP_USER is not set"); | ||
invariant(process.env.SMTP_PASS, "SMTP_PASS is not set"); | ||
|
||
return nodemailer.createTransport({ | ||
host: process.env.SMTP_HOST, | ||
port: Number.parseInt(process.env.SMTP_PORT, 10), | ||
secure: process.env.SMTP_SECURE === "true", | ||
auth: { | ||
user: process.env.SMTP_USER, | ||
pass: process.env.SMTP_PASS, | ||
}, | ||
}); | ||
} catch (error) { | ||
throw new EmailConfigurationError( | ||
`Invalid email configuration: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
} | ||
|
||
export async function sendEmail( | ||
subject: string, | ||
body: string, | ||
recipients: EmailRecipient[], | ||
): Promise<void> { | ||
if (recipients.length === 0) { | ||
throw new EmailSendError("No recipients found"); | ||
} | ||
const to = recipients | ||
.map((r) => `${r.userDisplayName} <${r.userEmail}>`) | ||
.join(", "); | ||
|
||
if (process.env.SEND_EMAIL_DEBUG === "1") { | ||
console.log("========= Email Debug Mode ========="); | ||
console.log("To:", to); | ||
console.log("Subject:", subject); | ||
console.log("Body:", body); | ||
console.log("=================================="); | ||
return; | ||
} | ||
|
||
const transporter = createTransport(); | ||
try { | ||
invariant(process.env.SMTP_FROM, "SMTP_FROM is not set"); | ||
const from = `${process.env.SMTP_FROM_NAME ?? ""} <${process.env.SMTP_FROM}>`; | ||
await transporter.sendMail({ | ||
from, | ||
to, | ||
subject, | ||
text: body, | ||
}); | ||
} catch (error) { | ||
throw new EmailSendError( | ||
"Failed to send email", | ||
error instanceof Error ? error : undefined, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface EmailRecipient { | ||
userDisplayName: string; | ||
userEmail: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.