Fix crash in receiveEmail when no R2 bucket binding is found - #148
Merged
Conversation
If no R2 bucket is configured via emailRouting.targetBucket and no bucket binding exists in the environment, the email handler would crash with a TypeError when calling bucket.put() on undefined. This adds a guard that throws a descriptive error message instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
r2-explorer-docs | 705622c | Commit Preview URL Branch Preview URL |
Mar 08 2026, 01:08 AM |
G4brym
commented
Mar 8, 2026
G4brym
left a comment
Owner
Author
There was a problem hiding this comment.
Automated Code Review — APPROVED ✅
Review Scores: 5/5 reviewers approved
Summary
Reviewed the addition of a null guard for the R2 bucket binding in receiveEmail(). This is a clean, minimal defensive fix that replaces an opaque TypeError crash with a descriptive, actionable error message. The change is correctly placed after both bucket discovery attempts and before any bucket usage.
Review Perspectives
- Correctness: ✅ Guard is in the right position, covers the exact gap between discovery and usage
- Security: ✅ Static error string, no user input in the message, no exposure risks
- Performance: ✅ Single boolean check, zero overhead
- Code Quality: ✅ Follows existing patterns, clear error message, proper changeset
- Testing: ✅ All 188 tests pass; the guard replaces one crash with a better one
Minor notes (non-blocking)
- A unit test covering the no-bucket-binding scenario would be a nice addition in the future, but is not required for this defensive fix.
🤖 Automated review by prodboard
This was referenced Mar 9, 2026
Merged
This was referenced Mar 16, 2026
This was referenced Apr 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
receiveEmail()to prevent aTypeErrorcrash when no R2 bucket binding is availableemailRouting.targetBucketresolves to a valid bucket nor any R2-like binding is found in the environment, the handler now throws a descriptiveErrorinstead of crashing onbucket.put()with an opaqueTypeError: Cannot read properties of undefinedWhat changed
In
packages/worker/src/modules/emails/receiveEmail.ts, after the two bucket discovery attempts (explicit config and environment scan), a guard clause was added:Why
Without this check, if a worker has email routing enabled but no R2 bucket binding configured, incoming emails would crash the worker with an unhelpful
TypeError. The new error message tells the operator exactly what's wrong and how to fix it.Test plan
🤖 Generated with Claude Code