mirror of
				https://github.com/goldbergyoni/nodebestpractices.git
				synced 2025-11-01 01:56:06 +08:00 
			
		
		
		
	Merge pull request #7 from i0natan/i0natan-patch-4
Unhandled rejections now do leave trace and warn
This commit is contained in:
		| @ -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 developer’s discpline is somewhat fragile. Consequently, it’s 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 developer’s discpline is somewhat fragile. Consequently, it’s 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) => | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Yoni Goldberg
					Yoni Goldberg