This repository was archived by the owner on Aug 31, 2024. It is now read-only.
Do not swallow errors thrown in callback #10
Closed
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.
I had an issue where a synchronous error thrown in the callback I passed to toArray was swallowed and never reported to me like I would expect. This happens because the callback executes in a promise.then() callback stack trace. The promise implementation catches the error and doesn't report it other than via the "unhandledRejection" event. I am not using promises in my application code so I don't have an "unhandledRejection" handler (and wasn't even aware of it). It took me a while to track down that an error was even being thrown before I tracked down where it was being swallowed.
This change uses process.nextTick to remove the callback from the promise.then call stack. I'm not sure if this is the best way to make sure the error gets reported. I'm not super familiar with promises but maybe it'd be better if an explicit error handler was registered on the promise and the error was re-thrown by that handler.
I also added a test using trycatch to make sure that the thrown error is reported up the stack as expected by a callback user.