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 9141705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:40:51+00:00 2026-06-17T09:40:51+00:00

I’m currently working on a node.js app and I’m having the usual asynchronous code

  • 0

I’m currently working on a node.js app and I’m having the usual asynchronous code issue.

I’m implementing a service server on top of Node’s HTTP module.

This server supports (express like) routes.
For example I have code that looks like this:

server.any("/someRoute",function(req,resp){
    resp.end("this text is sent to clients via http")
});

The server needs to be able to withstand failure, I do not want to crash the whole server when there is a problem in a function passed to any. The problem occurs when I’m writing code that looks like:

server.any("/someRoute",function(req,resp){
    setTimeout(function(){
        throw new Error("This won't get caught");
    },100);
});

I don’t see how I possible can catch the error here. I don’t want to crash the server over one server-side glitch, instead I want to serve 500.

The only solutions I’ve been able to come up with are really not expressive. I’ve only come up with using process.on("uncaughtException",callback) and similar code using node 0.8 Domains (which is a partial remedy but Domains are currently buggy and this is still not very expressive since I end up having to create a domain for every handle).

What I would like to accomplish is binding throw actions from a function to a scope, the ideal solution is something like binding all thrown errors from a function to a specific handler function.

Is this possible? What is the best practice to handle errors in this case?

I’d like to emphasise that it should be able to continue serving requests after a bad requests, and restarting the server on every request or creating domains for every handler and catching their uncaught exceptions seems like a bad idea to me. Additionally – I’ve heard promises might be able to assist me (something about throw in promises), can promises aid me in this situation?

  • 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-17T09:40:51+00:00Added an answer on June 17, 2026 at 9:40 am

    Warning: I would not recommend the original answer using domains, domains are being deprecated in the future, I had a lot of fun writing the original answer but I no longer believe it is too relevant. Instead – I suggest using event emitters and promises that have better error handling – here is the below example with promises instead. The promises used here are Bluebird:

    Promise.try(function(){ 
        throw new Error("Something");
    }).catch(function(err){
        console.log(err.message); // logs "Something"
    });
    

    With a timeout (note that we have to return the Promise.delay):

    Promise.try(function() {
        return Promise.delay(1000).then(function(){
            throw new Error("something");
        });
    }).catch(function(err){
        console.log("caught "+err.message);
    });
    

    With a general NodeJS funciton:

    var fs = Promise.promisifyAll("fs"); // creates readFileAsync that returns promise
    fs.readFileAsync("myfile.txt").then(function(content){
        console.log(content.toString()); // logs the file's contents
        // can throw here and it'll catch it
    }).catch(function(err){
        console.log(err); // log any error from the `then` or the readFile operation
    });
    

    This approach is both fast and catch safe, I recommend it above the below answer which uses domains that are likely not here to stay.


    I ended up using domains, I have created the following file I called mistake.js which contains the following code:

    var domain=require("domain");
    module.exports = function(func){
        var dom = domain.create();
        return { "catch" :function(errHandle){
            var args = arguments;
            dom.on("error",function(err){
                return errHandle(err);
            }).run(function(){
                func.call(null, args);
            });
            return this;
        };
    };
    

    Here is some example usage:

    var atry = require("./mistake.js");
    
    atry(function() {
        setTimeout(function(){
            throw "something";
        },1000);
    }).catch(function(err){
        console.log("caught "+err);
    });
    

    It also works like normal catch for synchronous code

    atry(function() {
        throw "something";
    }).catch(function(err){
        console.log("caught "+err);
    });
    

    I would appreciate some feedback on the solution

    On a side note, in v 0.8 apparently when you catch the exception in the domain it still bubbles to process.on("uncaughtException"). I dealt with this in my process.on("uncaughtException") with

     if (typeof e !== "object" || !e["domain_thrown"]) {
    

    However, the documentation suggests against process.on("uncaughtException") any way

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you

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.