RobKohr
RobKohr

Reputation: 6943

No error being thrown for undefined variable in node.js with express

I am running node.js with express. I wrote a node module with methods in it so when you go to http://bla.com/module_name/method_name it will run the method.

The method follows the typical style of

exports.method_name(req, res, next);

my main app does something like this:

app.all("*", resSetup, controller, render);

and controller is the thing that will call the method based on the path.

it seems that if there is an undefined variable error in the method, express will just hang there and not throw any error. Nothing will appear in the console log either. I can put a console message right before and after where the error occurs and the before will appear in the log, and after will not.

I can wrap it in a try/catch and get this:

[ReferenceError: blabla is not defined]

but no line numbers or anything.

My guess is that express is somehow preventing the errors from coming up. When I put the error in the function called "controller" that is directly in the route, it shows that error correctly.

It might not matter too much, but here is the code I am working on:

https://github.com/RobKohr/quick-site/blob/master/index.js

Line 189 is where the method call happens.

Upvotes: 12

Views: 2961

Answers (2)

kierans
kierans

Reputation: 2213

  1. Express heavily relies on Nodes asynchronous nature. Seeing errors thrown like on line 30 would give me the creeps if I was maintaining this. Try refactoring your code to only use the next(err) pattern.

  2. The reason that you app is hanging is that Express hasn't finished the HTTP response (eg: res.send()). This means you have broken plumbing where an Error has bubbled up the call stack but not redirected into the Express middleware pipeline. Try registering some error middleware to see if it gets called with your error.

Upvotes: 0

Mick
Mick

Reputation: 25471

Building on Ruairi's comment above, I had this same issue with when using 'q' (https://github.com/kriskowal/q) and promises with express - node would hang and no error was generated.

By adding a catch to the end of the promise 'callback' chain I was able to see the error and print it to console etc.

The code ends up looking like:

export function index(req, res) {

    //Create the 'promise'
    var request = req.body;
    var queryJobID = req.query.jobId;
    console.log('queryJobID: ' + queryJobID);
    var jobStatusPromsie = jobManager.job.getStatus(queryJobID);

    Q.all([jobStatusPromsie])
    .then(
        function (result) {
            var responseData = {};
            console.log('Job Status Response received');
            if (result != null) {
                //Without the .catch below an error here will be 'silent'
                console.log('jobStatus result: ' + util.inspect(result, false, null));
                responseData['status'] = 'OK';
                responseData['progress'] = result.progress;
                res.json(responseData);
            } else {
                console.log('jobStatus Error');
                responseData['status'] = 'Error';
            }
            res.json(responseData);
            console.log('jobStatus Response data sent');
        },
        function (error) {
            console.log('Error while getting job status:', error);
            res.json("Error");
        })
    .catch(function(err) {
        //handle errors
        console.log('Promise error while getting job status:', err);
    }); 
}

Upvotes: 2

Related Questions