stack/apps/backend/src/lib
Aman Ganapathy abc320b4db
[Refactor] [Fix] Email Rendering Pipeline Refactor, Error Handling, and Bug Fixes (#1140)
### Context
We noticed some errors pop up on sentry related to email rendering.
These errors seem to have been triggered by the same issue, and could be
categorized as follows:

1. Sanity test mismatch, even when the errors from freestyle and vercel
sandbox were broadly similar. This occurred due to stack traces
differing in different execution environments.
2. Rendering errors from freestyle and vercel sandbox caused by the
theme not being imported/ empty theme component.

Upon investigation, this occurred because hitting save on the email
themes page with an invalid theme (ex: deleting the `export` keyword, or
renaming the `EmailTheme` component) still triggers `bundleAndExecute`
with the invalid themes. This will obviously fail and cause the errors
to be logged, however there is no cause for concern here because the
error is returned and the save is denied because an error is returned.
It's more of a matter of noisy error logs and too strict sanity test
comparisons.

Beyond that, `js-execution` is a little opaque and hard to understand,
and this can mask errors in logic.

We also noticed a new issue: manually throwing an error in the email
theme code editor, and then trying to save was actually successful. This
was because the version of `react-email/components` we were using had
faulty error handling, and fell back to client side rendering, masking
the error. This wasn't caught by our `try-catch` safeguards because it
was a render time issue that was masked. More specifically, this was
what `react-email` was doing: `Switched to client rendering because the
server rendering errored`.

### Summary of Changes

We loosen the sanity test comparison between engine execution results in
case of errors. We then refactor the `js-execution` and
`email-rendering` files to read better, and to only `captureError` when
a service is down, but not for runtime errors in the user submitted
code.

To deal with the other bug, we bumped `react-email/components` to the
latest version. However, doing so exposed a gap between real `freestyle`
and our `freestyle-mock`: with the mock, the errors that were now raised
were treated as uncaught exceptions, crashing the mock server.
Consequently, we switched to using `node` over `bun`.

We also expanded test coverage to account for different error paths. 

Co-authored-by: Konstantin Wohlwend <n2d4xc@gmail.com>
2026-02-02 17:35:51 -08:00
..
ai-chat Email Drafts (#849) 2025-09-10 22:57:18 +00:00
cache.tsx Upgrade Prisma to v7 (#1064) 2025-12-26 08:13:34 -08:00
clickhouse.tsx query timing route (#1146) 2026-02-02 16:07:59 -08:00
config.tsx [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00
contact-channel.tsx Onboarding app & restricted users (#1069) 2026-01-11 17:22:14 -08:00
dev-perf-stats.tsx Rename dev-perf-history global 2026-01-12 20:58:59 -08:00
dev-request-stats.tsx Fix dev stats accuracy 2025-12-29 17:15:08 +01:00
email-delivery-stats.tsx Make most queries readonly 2026-01-02 04:25:05 +01:00
email-drafts.tsx fix types 2025-12-26 18:11:37 +01:00
email-queue-step.tsx Domain stopgaps 2026-01-30 16:20:41 -08:00
email-rendering.test.tsx [Fix]: Add better and more informative error handling to email-rendering engines (#1124) 2026-01-20 12:38:03 -08:00
email-rendering.tsx [Refactor] [Fix] Email Rendering Pipeline Refactor, Error Handling, and Bug Fixes (#1140) 2026-02-02 17:35:51 -08:00
emails-low-level.tsx Email outbox CRUD endpoints (#1062) 2026-01-06 16:54:47 -08:00
emails.tsx Upgrade Prisma to v7 (#1064) 2025-12-26 08:13:34 -08:00
end-users.tsx Upgrade backend to Next.js 15 (#360) 2024-12-10 00:11:13 -08:00
events.tsx Remove is_wide from Clickhouse events 2026-01-28 11:34:56 -08:00
images.tsx Project logo upload (#817) 2025-08-19 04:45:16 +02:00
internal-api-keys.tsx Make most queries readonly 2026-01-02 04:25:05 +01:00
js-execution.tsx [Refactor] [Fix] Email Rendering Pipeline Refactor, Error Handling, and Bug Fixes (#1140) 2026-02-02 17:35:51 -08:00
notification-categories.ts Email outbox backend (#1030) 2025-12-12 10:26:38 -08:00
oauth.tsx [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00
openapi.tsx [Docs][Content] API/SK docs for payments (#935) 2025-10-11 14:47:25 -07:00
payments.test.tsx fix payment data integrity (#1137) 2026-01-28 09:01:43 -08:00
payments.tsx fix payment data integrity (#1137) 2026-01-28 09:01:43 -08:00
permissions.tsx fix types 2025-12-26 18:11:37 +01:00
projects.tsx [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00
redirect-urls.test.tsx [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00
redirect-urls.tsx [Fix] [Feat] Update OAuth Sign-In and Get Token Functions to Work (#1130) 2026-01-28 02:17:27 +00:00
request-checks.tsx Upgrade Prisma to v7 (#1064) 2025-12-26 08:13:34 -08:00
stripe-proxy.tsx one time payments (#865) 2025-09-12 17:14:09 -07:00
stripe.tsx payment email templates (#1106) 2026-01-20 18:45:01 -08:00
telegram.tsx payment email templates (#1106) 2026-01-20 18:45:01 -08:00
tenancies.tsx Make most queries readonly 2026-01-02 04:25:05 +01:00
tokens.tsx Reduce number of token refresh events 2026-01-28 14:56:11 -08:00
types.tsx Upgrade Prisma to v7 (#1064) 2025-12-26 08:13:34 -08:00
upstash.tsx Email outbox backend (#1030) 2025-12-12 10:26:38 -08:00
users.tsx Improved anonymous users (#857) 2025-08-24 11:36:01 -07:00
webhooks.tsx Rename USER to PROJECT in permissions (#576) 2025-03-27 09:39:48 -07:00