diff --git a/apps/backend/src/lib/email-rendering.tsx b/apps/backend/src/lib/email-rendering.tsx index aa8597bbe5..f1116eb54c 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,21 @@ 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}`); + } + if (!executeResult.data.result) { + const noResultError = new StackAssertionError("No result from Freestyle", { + executeResult, + templateOrDraftComponent, + themeComponent, + options, + }); + captureError("freestyle-no-result", noResultError); + throw noResultError; } - return Result.ok(output.data.result as { html: string, text: string, subject: string, notificationCategory: string }); + return Result.ok(executeResult.data.result as { html: string, text: string, subject: string, notificationCategory: string }); } export async function renderEmailsWithTemplateBatched( @@ -205,6 +215,16 @@ export async function renderEmailsWithTemplateBatched( if (executeResult.status === "error") { return Result.error(executeResult.error); } + if (!executeResult.data.result) { + const noResultError = new StackAssertionError("No result from Freestyle", { + executeResult, + templateOrDraftComponent, + themeComponent, + inputs, + }); + captureError("freestyle-no-result", noResultError); + throw noResultError; + } return Result.ok(executeResult.data.result as Array<{ html: string, text: string, subject?: string, notificationCategory?: string }>); }