Merge pull request #7 from i0natan/i0natan-patch-4

Unhandled rejections now do leave trace and warn
This commit is contained in:
Yoni Goldberg
2017-10-31 18:39:34 +02:00
committed by GitHub

View File

@ -4,11 +4,11 @@
### One Paragraph Explainer
Typically, most of modern Node.JS/Express application code runs within promises whether within the .then handler, a function callback or in a catch block. Suprisingly, unless a developer remembered to add a .catch clause, errors thrown at these places disappear without leaving any trace(!). They will not get caught even by app.uncaughtException. The straighforward solution is to never forget adding .catch clause within each promise chain call and redirect to a centralized error handler. However building your error handling strategy only on developers discpline is somewhat fragile. Consequently, its highly recommended using a graceful fallback and subscribe to process.on(unhandledRejection, callback) this will ensure that any promise error, if not handled locally, will get its treatment.
Typically, most of modern Node.JS/Express application code runs within promises whether within the .then handler, a function callback or in a catch block. Suprisingly, unless a developer remembered to add a .catch clause, errors thrown at these places disappear, even not by app.uncaughtException. Recent versions of Node added a warning message when an unhandled rejection pops, though this might help to notice when things go wrong but it's obviously not a proper error handling. The straighforward solution is to never forget adding .catch clause within each promise chain call and redirect to a centralized error handler. However building your error handling strategy only on developers discpline is somewhat fragile. Consequently, its highly recommended using a graceful fallback and subscribe to process.on(unhandledRejection, callback) this will ensure that any promise error, if not handled locally, will get its treatment.
<br/><br/>
### Code example: Shockingly, these errors will leave no trace
### Code example: these errors will not get caught by any error handler (except unhandledRejection)
```javascript
DAL.getUserById(1).then((johnSnow) =>