Commit 37472e6
authored
Ensure Promise rejections include stack traces (#3738)
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/69171 parent 135598b commit 37472e6
1 file changed
+3
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
933 | 933 | | |
934 | 934 | | |
935 | 935 | | |
936 | | - | |
| 936 | + | |
937 | 937 | | |
938 | 938 | | |
939 | 939 | | |
| |||
970 | 970 | | |
971 | 971 | | |
972 | 972 | | |
973 | | - | |
| 973 | + | |
974 | 974 | | |
975 | | - | |
| 975 | + | |
976 | 976 | | |
977 | 977 | | |
978 | 978 | | |
| |||
0 commit comments