Throw not calling catch method
See original GitHub issueVersion: 3.3.4 Node.js: 5.60
throw new Exception(Exception.database);
results in following error: Unhandled rejection (<{“id”:1,“msg”:“Database Exception”}>, no stack trace)
On the contrary
reject(new Exception(Exception.database);
causes no issues.
See code -
class Database{
static cypher(query, params){
return new Promise(function(fulfill, reject){
db.cypher({
query: query,
params: params
})
.then(function(data){
fulfill(data);
})
.catch(function(){
throw new Exception(Exception.database); // Point A
})
});
}
}
class User{
// Constructor .etc
login(){
var me = this;
return new Promise(function(fulfill, reject){
Database.cypher(me.login_query, me.login_params)
.then(function(data){
// Returns empty result
if(!data[0]){
throw new Exception(Exception.user_credentials);
}
// Assign values
data = data[0].user.properties;
me.username = data.username;
me.firstname = data.firstname;
me.lastname = data.lastname;
me.email = data.email;
fulfill();
})
// Database exception
.catch(function(ex){
console.log('error caught'); // Point B
})
});
}
}
// construction .etc
x.login_query = 'invalid query here';
x.login(); // should print 'error caught' but instead prints 'unhandled rejection' despite catch
Code at Point B only executes when the throw at Point A is changed from a throw to a call of the reject function.
To my knowledge, bluebird should handle throws exactly the same as it handles calls to the reject function - that is, to pass errors to the promises catch function.
My issue is that bluebird sometimes sends throws to the catch function, but, at other times (like in the example above) it does not. It registers the throw as a rejection but does not call the catch function.
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Why cant I use a method that throws in a catch block? ...
First of all, it's important to understand that there are 2 types of exceptions: Checked exceptions; Unchecked exceptions.
Read more >Promise.prototype.catch() - JavaScript - MDN Web Docs
The catch() method of a Promise object schedules a function to be called when the promise is rejected. It immediately returns an equivalent ......
Read more >If a method is defined to throw an exception, do you always ...
If the exception is a checked exception, it must be thrown inside a try-catch that catches it OR the method the throwing code...
Read more >Error handling with async/await and promises, n² ways to ...
Will not catch errors thrown in another call stack via a setTimeout() or setInterval() callback. Yes, but only if the function was called...
Read more >Basic try-catch-finally Exception Handling in Java
If no exeception is thrown by any of the methods called or statements executed inside the try-block, the catch-block is simply ignored.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
new Promise
is not needed when you are dealing with APIs that already return a promise. You can write this equivalent code (for more, see explicit construction anti-pattern):Your throw statement is indeed inside a different function, that’s why it’s not affecting the upper function:
Thank you all for the feedback, apologies for any wasted time.