I\'ve structured my Node.js application around the async/await library and it has been working great most of the time. The only trouble I have with it is that whenever a promise
I suggest you set a global unhandledRejection handler at the very beginning of your entry file:
process.on('unhandledRejection', (reason, p) => { throw reason });
This way, even if you forget to catch the errors locally, you can still track them down easily.
Update
There seems to be some confusion as to how the above handler helps you. Basically, when you don't catch promise errors, node outputs that warning to the console. For whatever silly reason, node only outputs the error message without the stack. Setting up the handler and then rethrowing the error generates the stack and allows you to debug your code easier. Here's an example:
let test = () => new Promise((resolve, reject) => {
throw new Error('Random Error'); // same as "reject(new Error('Random Error'));"
});
test();
Without the handler you get:
(node:20012) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Random Error
Then, we add the handler at the top of our file:
process.on('unhandledRejection', (reason, p) => { throw reason });
let test = () => new Promise((resolve, reject) => {
throw new Error('Random Error'); // same as "reject(new Error('Random Error'));"
});
test();
Now we get a much nicer error stack:
(function (exports, require, module, __filename, __dirname) { process.on('unhandledRejection', (reason, p) => { throw reason });
^
Error: Random Error
at Promise (S:\amir\test.js:5:9)
at test (S:\amir\test.js:3:18)
at Object.<anonymous> (S:\amir\test.js:8:1)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
The warning is caused by a an error happened in one of your promises, but you are not handling it, this means your promise is not handling the catch
as well as you are handling then
.
Its just a good practice to handle promise catch
as well as you are doing with then
so no matter what is the situation you need to keep in mind to handle errors even if you are 100% sure this promise will not cause an error.
This will give you a better and faster way to debug any issue .... so for any promise just handle the catch
example
promise.then((result)=>{
//Do something here
} , (error) =>{
//Handle promise rejection
}).catch((err) => {
//Handle error here, lets say for example, this promise is just updating user
//console.log("update user error")
//console.log(err); to be able to understand what is the error
})
So if you used the above way to handle any promise ... you will be able to know where exactly is your error ...
Also one thing that i usually do is to console.log
what the promise is doing before console.log
the error, as you can see in the code above i am considering that this promise is just updating a user ... so i mention in the catch "update user error"
Now you know this error is inside the update user promise