-
Notifications
You must be signed in to change notification settings - Fork 498
[Fix]: Add better and more informative error handling to email-rendering engines #1124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. 📝 WalkthroughWalkthroughAdds Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant EmailRenderer
participant FreestyleEngine
participant VercelSandbox
participant ResultStore
Caller->>EmailRenderer: renderEmailWithTemplate(template, vars, opts)
EmailRenderer->>FreestyleEngine: execute template (freestyle)
alt Freestyle succeeds
FreestyleEngine-->>EmailRenderer: { status: "ok", data }
EmailRenderer->>ResultStore: persist/return OK result
ResultStore-->>Caller: OK response
else Freestyle fails
FreestyleEngine-->>EmailRenderer: { status: "error", error } (includes stack)
EmailRenderer->>VercelSandbox: fallback run (provide innerCode, innerOptions)
alt Sandbox succeeds
VercelSandbox-->>EmailRenderer: { status: "ok", data }
EmailRenderer->>ResultStore: persist/return OK result
ResultStore-->>Caller: OK response
else Sandbox fails
VercelSandbox-->>EmailRenderer: { status: "error", error } (attached innerCode/innerOptions)
EmailRenderer-->>Caller: return error (JSON-stringified payload with message, stack, cause, inner metadata)
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
On Sentry, we saw an error with vercel sandbox. This was thrown because the runner script itself had no safeguards. Also, the error raised was uninformative. So we improve the error handling in the file.
2ffb861 to
a4dc1a0
Compare
Greptile OverviewGreptile SummaryThis PR improves error handling in the email rendering pipeline by adding structured error objects with stack traces and comprehensive debugging context. Key Changes:
Impact: Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant EmailRendering
participant BundleAndExecute
participant JSExecution
participant FreestyleEngine
participant VercelSandbox
participant RunnerScript
Client->>EmailRendering: renderEmailsForTenancyBatched(requests)
EmailRendering->>BundleAndExecute: bundleAndExecute(files)
BundleAndExecute->>JSExecution: executeJavascript(code, options)
alt Freestyle execution
JSExecution->>FreestyleEngine: execute(code, options)
alt Success
FreestyleEngine-->>JSExecution: {status: "ok", data: result}
JSExecution-->>BundleAndExecute: result
BundleAndExecute-->>EmailRendering: Result.ok(data)
else Error
FreestyleEngine-->>JSExecution: {status: "error", error: {message, stack, cause}}
Note over JSExecution: Fallback to Vercel Sandbox
JSExecution->>VercelSandbox: execute(code, options)
end
end
alt Vercel Sandbox execution
VercelSandbox->>VercelSandbox: Create sandbox
VercelSandbox->>VercelSandbox: Install node modules
VercelSandbox->>RunnerScript: Write code.mjs and runner.mjs
VercelSandbox->>RunnerScript: Run node runner.mjs
alt Runner try-catch success
RunnerScript->>RunnerScript: import('./code.mjs')
RunnerScript->>RunnerScript: Execute function
alt Function success
RunnerScript->>RunnerScript: {status: "ok", data: result}
else Function error
RunnerScript->>RunnerScript: {status: "error", error: {message, stack, cause}}
end
RunnerScript->>RunnerScript: writeFileSync(result.json)
RunnerScript-->>VercelSandbox: exitCode: 0
else Runner crash
RunnerScript-->>VercelSandbox: exitCode: non-zero
Note over VercelSandbox: Unexpected failure (OOM, timeout, etc.)
end
VercelSandbox->>VercelSandbox: Read result.json
VercelSandbox->>VercelSandbox: JSON.parse(resultJson)
VercelSandbox-->>JSExecution: result or error
alt Vercel success
JSExecution-->>BundleAndExecute: result
BundleAndExecute->>BundleAndExecute: JSON.stringify(error)
BundleAndExecute-->>EmailRendering: Result.ok(data)
else Vercel error
JSExecution-->>BundleAndExecute: error
BundleAndExecute->>BundleAndExecute: JSON.stringify(error)
BundleAndExecute-->>EmailRendering: Result.error(string)
end
end
EmailRendering-->>Client: Result<EmailRenderResult[], string>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No files reviewed, no comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request enhances error handling across the email rendering pipeline by adding more informative error context and improving safeguards in the JavaScript execution engines. The changes were prompted by production errors in Sentry after deploying Vercel fallback and Freestyle improvements.
Changes:
- Added
innerCodeandinnerOptionscontext to all error logs throughout the execution pipeline for better debugging - Implemented try-catch wrapper in Vercel Sandbox runner script to handle errors gracefully instead of failing with exit codes
- Updated error structure from simple strings to objects containing
message,stack, andcauseproperties - Added comprehensive test coverage for error scenarios including bundling errors, runtime errors, and batch failure behavior
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
| apps/backend/src/lib/js-execution.tsx | Added error context (innerCode, innerOptions) to all StackAssertionError instances; implemented try-catch wrapper in Vercel Sandbox runner script to capture execution errors with stack traces |
| apps/backend/src/lib/email-rendering.tsx | Changed error structure from string to object with message/stack/cause; updated error serialization to use JSON.stringify for structured error objects |
| apps/backend/src/lib/email-rendering.test.tsx | Added new test cases for bundling errors, runtime errors (component throws, arktype validation), and batch failure scenarios; updated test descriptions for clarity |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
We had a small bug where when the vercel fallback failed, we still returned the freestyle failure. We also make errors more informative.
There is sufficient nuance/ complexity to dissuade merging the two renderEmailxxx functions. While the render-email.test.tsx covers the main path through renderEmailWithTemplate, preview mode is never explicitly tested.
a4dc1a0 to
cc82e50
Compare
We now read from a file buffer rather than cat. Cat returns the same result for an empty file and a nonexistent file. readFile makes it more explicit when the file wasn't created. We simplify runner to better match freestyle behavior
N2D4
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as discussed
Summary of Changes
We ran into an error on sentry when the vercel fallback and freestyle improvements were pushed to prod. Spiking into the error handling revealed that the errors could be more informative to enable easier debugging.
We improve the error handling and add extra test coverage to cover the error pathways through the code. Note that we do not test vercel sandbox itself nor the fallback mechanism-this is because a) these will be logged and tested in prod with the sanity test code, and b) creating a mock vercel sandbox instance the way we have a mock freestyle server would just slow down any tests that pass through the email rendering pipeline, all for something thats meant to just be a fallback. However, locally, we tested with scripts and real vercel sandbox test project to success. Note that we also tried running the existing email-rendering test suite with fake freestyle credentials and real vercel-sandbox credentials (to mimic the fallback) and they passed.
Summary by CodeRabbit
New Features
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.