question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw not calling catch method

See original GitHub issue

Version: 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:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
petkaantonovcommented, Mar 15, 2016

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):

class Database {
    static cypher(query, params){
        return db.cypher({
            query: query,
            params: params
        }).catch(function() {
            throw new Exception(Exception.database);
        });
    }
}

Your throw statement is indeed inside a different function, that’s why it’s not affecting the upper function:

.catch(function onlyThisFunctionIsAffectedByThrowsAndReturnsInsideThisFunction() {
    throw new Exception(Exception.database);
});
0reactions
andyrichardsoncommented, Mar 15, 2016

Thank you all for the feedback, apologies for any wasted time.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found