From 5841c3294bc8981df1398747f5f35f8e7eaaf2ef Mon Sep 17 00:00:00 2001 From: Bilal Godil Date: Tue, 21 Oct 2025 19:19:12 -0700 Subject: [PATCH 1/2] capture freestyle error --- apps/backend/src/lib/email-rendering.tsx | 28 +++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/apps/backend/src/lib/email-rendering.tsx b/apps/backend/src/lib/email-rendering.tsx index aa8597bbe5..face28b337 100644 --- a/apps/backend/src/lib/email-rendering.tsx +++ b/apps/backend/src/lib/email-rendering.tsx @@ -1,6 +1,6 @@ import { Freestyle } from '@/lib/freestyle'; import { emptyEmailTheme } from '@stackframe/stack-shared/dist/helpers/emails'; -import { StackAssertionError } from '@stackframe/stack-shared/dist/utils/errors'; +import { captureError, StackAssertionError } from '@stackframe/stack-shared/dist/utils/errors'; import { bundleJavaScript } from '@stackframe/stack-shared/dist/utils/esbuild'; import { get, has } from '@stackframe/stack-shared/dist/utils/objects'; import { Result } from "@stackframe/stack-shared/dist/utils/results"; @@ -120,11 +120,20 @@ export async function renderEmailWithTemplate( "@react-email/components": "0.1.1", "arktype": "2.1.20", }; - const output = await freestyle.executeScript(result.data, { nodeModules }); - if (output.status === "error") { - return Result.error(`${output.error}`); + const executeResult = await freestyle.executeScript(result.data, { nodeModules }); + if (executeResult.status === "error") { + return Result.error(`${executeResult.error}`); } - return Result.ok(output.data.result as { html: string, text: string, subject: string, notificationCategory: string }); + if (!executeResult.data.result) { + captureError("freestyle-no-result", new StackAssertionError("No result from Freestyle", { + executeResult, + templateOrDraftComponent, + themeComponent, + options, + })); + return Result.error("An unknown error occurred while rendering this email. Please try again"); + } + return Result.ok(executeResult.data.result as { html: string, text: string, subject: string, notificationCategory: string }); } export async function renderEmailsWithTemplateBatched( @@ -205,6 +214,15 @@ export async function renderEmailsWithTemplateBatched( if (executeResult.status === "error") { return Result.error(executeResult.error); } + if (!executeResult.data.result) { + captureError("freestyle-no-result", new StackAssertionError("No result from Freestyle", { + executeResult, + templateOrDraftComponent, + themeComponent, + inputs, + })); + return Result.error("An unknown error occurred while rendering this email. Please try again"); + } return Result.ok(executeResult.data.result as Array<{ html: string, text: string, subject?: string, notificationCategory?: string }>); } From 9b612c0a447e8f45d983ea5b3694dc51a456fb86 Mon Sep 17 00:00:00 2001 From: Bilal Godil Date: Mon, 27 Oct 2025 09:47:48 -0700 Subject: [PATCH 2/2] throw noResultError --- apps/backend/src/lib/email-rendering.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/backend/src/lib/email-rendering.tsx b/apps/backend/src/lib/email-rendering.tsx index face28b337..f1116eb54c 100644 --- a/apps/backend/src/lib/email-rendering.tsx +++ b/apps/backend/src/lib/email-rendering.tsx @@ -125,13 +125,14 @@ export async function renderEmailWithTemplate( return Result.error(`${executeResult.error}`); } if (!executeResult.data.result) { - captureError("freestyle-no-result", new StackAssertionError("No result from Freestyle", { + const noResultError = new StackAssertionError("No result from Freestyle", { executeResult, templateOrDraftComponent, themeComponent, options, - })); - return Result.error("An unknown error occurred while rendering this email. Please try again"); + }); + captureError("freestyle-no-result", noResultError); + throw noResultError; } return Result.ok(executeResult.data.result as { html: string, text: string, subject: string, notificationCategory: string }); } @@ -215,13 +216,14 @@ export async function renderEmailsWithTemplateBatched( return Result.error(executeResult.error); } if (!executeResult.data.result) { - captureError("freestyle-no-result", new StackAssertionError("No result from Freestyle", { + const noResultError = new StackAssertionError("No result from Freestyle", { executeResult, templateOrDraftComponent, themeComponent, inputs, - })); - return Result.error("An unknown error occurred while rendering this email. Please try again"); + }); + captureError("freestyle-no-result", noResultError); + throw noResultError; } return Result.ok(executeResult.data.result as Array<{ html: string, text: string, subject?: string, notificationCategory?: string }>); }