Skip to content

Ensure Promise rejections include stack traces#3738

Merged
SteffenDE merged 1 commit intophoenixframework:mainfrom
rhcarvalho:promise-reject-error
Apr 16, 2025
Merged

Ensure Promise rejections include stack traces#3738
SteffenDE merged 1 commit intophoenixframework:mainfrom
rhcarvalho:promise-reject-error

Conversation

@rhcarvalho
Copy link
Contributor

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:

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
}
},
error: (reason) => reject({error: reason}),
error: (reason) => reject(new Error(`failed with reason: ${reason}`)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rhcarvalho
Copy link
Contributor Author

For context, I came to these trying to isolate LiveView library errors from in-app errors caught by an unhandledrejection handler.

@SteffenDE SteffenDE merged commit 37472e6 into phoenixframework:main Apr 16, 2025
8 checks passed
@SteffenDE
Copy link
Collaborator

Thank you! 🙌🏻

@rhcarvalho rhcarvalho deleted the promise-reject-error branch September 16, 2025 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments