Ensure Promise rejections include stack traces#3738
Merged
SteffenDE merged 1 commit intophoenixframework:mainfrom Apr 16, 2025
Merged
Ensure Promise rejections include stack traces#3738SteffenDE merged 1 commit intophoenixframework:mainfrom
SteffenDE merged 1 commit intophoenixframework:mainfrom
Conversation
Modify instances where Promise.reject() was called with non-Error values (e.g., strings, plain objects) to consistently use Promise.reject(new Error(...)) instead. The main motivation is debugging where errors are coming from: 1. Stack Traces: Error objects automatically capture a stack trace when created. This trace shows exactly where in the code the rejection originated. Rejecting with strings or other primitives loses this vital information. 2. Consistent Error Handling: Downstream .catch() blocks or async/await try...catch blocks can reliably expect an object with standard error properties (.message, .stack, .name). Handling arbitrary rejected types (strings, numbers, objects) complicates error handling logic and can lead to runtime errors if code tries to access properties that don't exist. 3. Debugging Tools & Libraries: Many debugging tools, error reporting services (like Sentry) are optimized to work with Error objects. Using them ensures better integration and more informative reporting. 4. Predictability & Convention: It's a widely accepted best practice in JavaScript to reject promises with Error objects. Adhering to this convention makes the codebase more predictable and easier for developers to understand and maintain. References: - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject#description - https://forum.sentry.io/t/javascript-unhandledrejection-timeout/6917
rhcarvalho
commented
Mar 29, 2025
| } | ||
| }, | ||
| error: (reason) => reject({error: reason}), | ||
| error: (reason) => reject(new Error(`failed with reason: ${reason}`)), |
Contributor
Author
There was a problem hiding this comment.
Here I intentionally included a prefix to the reason for grep-ability, as otherwise the message would be solely dynamically coming from the server, making it harder to find.
| error: (reason) => reject(new Error(`failed with reason: ${reason}`)), | ||
| timeout: () => { | ||
| reject({timeout: true}) | ||
| reject(new Error("timeout")) |
Contributor
Author
There was a problem hiding this comment.
If one day it becomes important to differentiate errors downstream, we could either have custom error classes or error codes.
For now, I think we're using new Error(...) throughout the code base for simplicity.
Contributor
Author
|
For context, I came to these trying to isolate LiveView library errors from in-app errors caught by an |
SteffenDE
approved these changes
Apr 4, 2025
Harrison1
approved these changes
Apr 11, 2025
Collaborator
|
Thank you! 🙌🏻 |
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.
Modify instances where
Promise.reject()was called with non-Error values (e.g., strings, plain objects) to consistently usePromise.reject(new Error(...))instead.The main motivation is debugging where errors are coming from:
References: