Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8968957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:31:37+00:00 2026-06-15T17:31:37+00:00

If I throw an Error, express renders it nicely using the connect errorHandler middleware.

  • 0

If I throw an Error, express renders it nicely using the connect errorHandler middleware.

exports.list = function(req, res){
  throw new Error('asdf');
  res.send("doesn't get here because Error is thrown synchronously");
};

And when I throw an Error within a promise it will be ignored (this makes sense to me).

exports.list = function(req, res){
  Q = require('q');
  Q.fcall(function(){
    throw new Error('asdf');
  });
  res.send("we get here because our exception was thrown async");
};

However, If I throw an Error within a promise and call “done” node crashes, because the exception isn’t caught by the middleware.

exports.list = function(req, res){
  Q = require('q');
  Q.fcall(function(){
    throw new Error('asdf');
  }).done();
  res.send("This prints. done() must not be throwing.");
};

After running the above, node crashes with this output:

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: asdf
    at /path/to/demo/routes/user.js:9:11

So my conclusion is that done() isn’t throwing the exception, but causes an exception to be thrown elsewhere. Is that right? Is there a way to accomplish what I’m trying – where errors in the promise will be handled by the middleware?

FYI: This hack will catch the exception at the top level, but it’s outside of the realm of middleware, so doesn’t suite my needs (to nicely render the error).

//in app.js #configure
process.on('uncaughtException', function(error) {
  console.log('uncaught expection: ' + error);
})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:31:38+00:00Added an answer on June 15, 2026 at 5:31 pm

    Maybe you’ll find connect-domain middleware useful for handling async errors. This middleware allows you to handle async errors like a regular errors.

    var
        connect = require('connect'),
        connectDomain = require('connect-domain');
    
    var app = connect()
        .use(connectDomain())
        .use(function(req, res){
            process.nextTick(function() {
                // This async error will be handled by connect-domain middleware
                throw new Error('Async error');
                res.end('Hello world!');
            });
        })
        .use(function(err, req, res, next) {
            res.end(err.message);
        });
    
    app.listen(3131);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app: Node.js, Express, some middleware including connect-assets and express.static. All running on local
i have an importante question with this code form .on('error', function(err) { throw err;
I am using express 2.5.8 on ubuntu 11 (amazon aws). I have connect 1.7.2
int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
I am getting null-exception throw error in the following code. What can be the
I need to throw 404 error in module. Or may be there are any
How do you throw an error from within an MSBuild task and force the
The following will throw the error Error in v$a : $ operator is invalid
This code does not throw an error but the query fails, that is, the
i asked a question here throw 404 error when route is not satisfied and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.