Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add better logging to resend

Marc Seitz6194b6332696
diff --git a/lib/emails/send-deleted-domain.ts b/lib/emails/send-deleted-domain.ts
index 7ae66edc..239d30e9 100644
--- a/lib/emails/send-deleted-domain.ts
+++ b/lib/emails/send-deleted-domain.ts
@@ -3,11 +3,15 @@ import DeletedDomainEmail from "@/components/emails/deleted-domain";
export const sendDeletedDomainEmail = async (email: string, domain: string) => {
const emailTemplate = DeletedDomainEmail({ domain });
- await sendEmail({
- to: email,
- subject: `Your domain ${domain} has been deleted`,
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- system: true,
- });
+ try {
+ await sendEmail({
+ to: email,
+ subject: `Your domain ${domain} has been deleted`,
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ system: true,
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/emails/send-email-verification.ts b/lib/emails/send-email-verification.ts
index 06e23c60..0e55d51a 100644
--- a/lib/emails/send-email-verification.ts
+++ b/lib/emails/send-email-verification.ts
@@ -6,11 +6,15 @@ export const sendVerificationEmail = async (
verificationURL: string,
) => {
const emailTemplate = EmailVerification({ verificationURL, email });
- await sendEmail({
- to: email,
- subject: "Verify your email address",
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- system: true,
- });
+ try {
+ await sendEmail({
+ to: email,
+ subject: "Verify your email address",
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ system: true,
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/emails/send-invalid-domain.ts b/lib/emails/send-invalid-domain.ts
index e4ca995c..d6c0a540 100644
--- a/lib/emails/send-invalid-domain.ts
+++ b/lib/emails/send-invalid-domain.ts
@@ -7,11 +7,15 @@ export const sendInvalidDomainEmail = async (
invalidDays: number,
) => {
const emailTemplate = InvalidDomainEmail({ domain, invalidDays });
- await sendEmail({
- to: email,
- subject: `Your domain ${domain} needs to be configured`,
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- system: true,
- });
+ try {
+ await sendEmail({
+ to: email,
+ subject: `Your domain ${domain} needs to be configured`,
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ system: true,
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/emails/send-teammate-invite.ts b/lib/emails/send-teammate-invite.ts
index adbefc65..107a5e4f 100644
--- a/lib/emails/send-teammate-invite.ts
+++ b/lib/emails/send-teammate-invite.ts
@@ -14,16 +14,20 @@ export const sendTeammateInviteEmail = async ({
to: string;
url: string;
}) => {
- await sendEmail({
- to: to,
- subject: `You are invited to join a ${senderName}'s team`,
- react: TeamInvitation({
- senderName,
- senderEmail,
- teamName,
- url,
- }),
- test: process.env.NODE_ENV === "development",
- system: true,
- });
+ try {
+ await sendEmail({
+ to: to,
+ subject: `You are invited to join a ${senderName}'s team`,
+ react: TeamInvitation({
+ senderName,
+ senderEmail,
+ teamName,
+ url,
+ }),
+ test: process.env.NODE_ENV === "development",
+ system: true,
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/emails/send-trial-end-final-reminder.ts b/lib/emails/send-trial-end-final-reminder.ts
index 85d5fa72..e46c1556 100644
--- a/lib/emails/send-trial-end-final-reminder.ts
+++ b/lib/emails/send-trial-end-final-reminder.ts
@@ -6,11 +6,15 @@ export const sendTrialEndFinalReminderEmail = async (
name: string | null,
) => {
const emailTemplate = TrialEndFinalReminderEmail({ name });
- await sendEmail({
- to: email,
- subject: `Your pro trial expires in 24 hours`,
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- system: true,
- });
+ try {
+ await sendEmail({
+ to: email,
+ subject: `Your pro trial expires in 24 hours`,
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ system: true,
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/emails/send-trial-end-reminder.ts b/lib/emails/send-trial-end-reminder.ts
index 7aa37707..ac0e8e23 100644
--- a/lib/emails/send-trial-end-reminder.ts
+++ b/lib/emails/send-trial-end-reminder.ts
@@ -6,11 +6,15 @@ export const sendTrialEndReminderEmail = async (
name: string | null,
) => {
const emailTemplate = TrialEndReminderEmail({ name });
- await sendEmail({
- to: email,
- subject: `Your pro trial is ending soon`,
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- system: true,
- });
+ try {
+ await sendEmail({
+ to: email,
+ subject: `Your pro trial is ending soon`,
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ system: true,
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/emails/send-upgrade-plan.ts b/lib/emails/send-upgrade-plan.ts
index afceb87d..bc4790e4 100644
--- a/lib/emails/send-upgrade-plan.ts
+++ b/lib/emails/send-upgrade-plan.ts
@@ -5,10 +5,14 @@ import { CreateUserEmailProps } from "../types";
export const sendUpgradePlanEmail = async (params: CreateUserEmailProps) => {
const { name, email } = params.user;
const emailTemplate = UpgradePlanEmail({ name });
- await sendEmail({
- to: email as string,
- subject: "Thank you for upgrading to Papermark Pro!",
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- });
+ try {
+ await sendEmail({
+ to: email as string,
+ subject: "Thank you for upgrading to Papermark Pro!",
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/emails/send-verification-request.ts b/lib/emails/send-verification-request.ts
index e3f3f07c..234a01f6 100644
--- a/lib/emails/send-verification-request.ts
+++ b/lib/emails/send-verification-request.ts
@@ -7,10 +7,14 @@ export const sendVerificationRequestEmail = async (params: {
}) => {
const { url, email } = params;
const emailTemplate = LoginLink({ url });
- await sendEmail({
- to: email as string,
- subject: "Welcome to Papermark!",
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- });
+ try {
+ await sendEmail({
+ to: email as string,
+ subject: "Welcome to Papermark!",
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/emails/send-viewed-document.ts b/lib/emails/send-viewed-document.ts
index 5c9def64..5cd1b5fe 100644
--- a/lib/emails/send-viewed-document.ts
+++ b/lib/emails/send-viewed-document.ts
@@ -17,11 +17,18 @@ export const sendViewedDocumentEmail = async ({
documentName,
viewerEmail,
});
- await sendEmail({
- to: email,
- subject: `Your document has been viewed: ${documentName}`,
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- system: true,
- });
+ try {
+ const data = await sendEmail({
+ to: email,
+ subject: `Your document has been viewed: ${documentName}`,
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ system: true,
+ });
+
+ // If sendEmail is successful, return the data
+ return { success: true, data };
+ } catch (error) {
+ return { success: false, error };
+ }
};
diff --git a/lib/emails/send-welcome.ts b/lib/emails/send-welcome.ts
index 7cba4474..2da9f53c 100644
--- a/lib/emails/send-welcome.ts
+++ b/lib/emails/send-welcome.ts
@@ -5,10 +5,14 @@ import { CreateUserEmailProps } from "../types";
export const sendWelcomeEmail = async (params: CreateUserEmailProps) => {
const { name, email } = params.user;
const emailTemplate = WelcomeEmail({ name });
- await sendEmail({
- to: email as string,
- subject: "Welcome to Papermark.io!",
- react: emailTemplate,
- test: process.env.NODE_ENV === "development",
- });
+ try {
+ await sendEmail({
+ to: email as string,
+ subject: "Welcome to Papermark.io!",
+ react: emailTemplate,
+ test: process.env.NODE_ENV === "development",
+ });
+ } catch (e) {
+ console.error(e);
+ }
};
diff --git a/lib/resend.ts b/lib/resend.ts
index b78cddb5..9eba4e42 100644
--- a/lib/resend.ts
+++ b/lib/resend.ts
@@ -1,4 +1,4 @@
-import { nanoid } from "@/lib/utils";
+import { log, nanoid } from "@/lib/utils";
import { ReactElement, JSXElementConstructor } from "react";
import { Resend } from "resend";
@@ -22,20 +22,40 @@ export const sendEmail = async ({
test?: boolean;
}) => {
if (!resend) {
- return Promise.resolve();
+ // Throw an error if resend is not initialized
+ throw new Error("Resend not initialized");
+ }
+
+ try {
+ const { data, error } = await resend.emails.send({
+ from: marketing
+ ? "Marc from Papermark <[email protected]>"
+ : system
+ ? "Papermark <[email protected]>"
+ : "Marc from Papermark <[email protected]>",
+ to: test ? "[email protected]" : to,
+ reply_to: marketing ? "[email protected]" : undefined,
+ subject,
+ react,
+ headers: {
+ "X-Entity-Ref-ID": nanoid(),
+ },
+ });
+
+ // Check if the email sending operation returned an error and throw it
+ if (error) {
+ throw error;
+ }
+
+ // If there's no error, return the data
+ return data;
+ } catch (exception) {
+ // Log and rethrow any caught exceptions for upstream handling
+ log({
+ message: `Unexpected error when sending email: ${exception}`,
+ type: "error",
+ mention: true,
+ });
+ throw exception; // Rethrow the caught exception
}
- return resend.emails.send({
- from: marketing
- ? "Marc from Papermark <[email protected]>"
- : system
- ? "Papermark <[email protected]>"
- : "Marc from Papermark <[email protected]>",
- to: test ? "[email protected]" : to,
- reply_to: marketing ? "[email protected]" : undefined,
- subject,
- react,
- headers: {
- "X-Entity-Ref-ID": nanoid(),
- },
- });
};