Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover from Uncaught Exception in Node.JS

Tags:

OK, so I have a problem. If an uncaught exception occurs while I am handling an HTTP request, I have no opportunity to call the end() method on the http.ServerResponse object. Therefore, the server hangs forever and never fulfills the request.

Here's an example:

var express = require('express');
var app = express.createServer();
var reqNum = 0;
app.get('/favicon.ico', function(req, res) {res.send(404);});
app.get('*', function(req, res, next) {
    console.log("Request #", ++reqNum, ":", req.url);
    next();
});
app.get('/error', function(req, res, next) {
    throw new Error("Problem occurred");
});
app.get('/hang', function(req, res, next) {
    console.log("In /hang route");
    setTimeout(function() {
        console.log("In /hang callback");
        if(reqNum >= 3)
            throw new Error("Problem occurred");
        res.send("It worked!");
    }, 2000);
});
process.on('uncaughtException', function(err) {
    console.log("Uncaught exception!", err);
});
app.listen(8080);

If you visit /error, an exception occurs, but it is caught. The user receives an error message - no problem. If I visit /hang, though, the server will eventually throw an uncaught exception and hang forever. Any subsequent requests for /hang will hang.

This sucks. Any advice for how to fix this issue?

like image 484
BMiner Avatar asked Nov 13 '11 21:11

BMiner


People also ask

What happens to uncaught errors in JS?

By default, causing an uncaught exception in Node. js results in printing the stack trace and exiting the process with code 1.

What is uncaught exception in JavaScript?

According to the Mozilla website for developer documents, “the TypeError object represents an error when a value is not of the expected type.” Uncaught means that the error was not caught in the catch part of the try-catch block.

What is Uncaughtexception?

June 01, 2022. CWE 248-Uncaught Exception occurs when an exception is not caught by a programming construct or by the programmer, it results in an uncaught exception. In Java, for example, this would be an unhandled exception that would terminate the program.


2 Answers

When an uncaught exception occurs, you're in an unclean state. Let the process die and restart it, there's nothing else you can do to safely bring it back to a known-good state. Use forever, it'll restart your process as soon as it dies.

like image 101
thejh Avatar answered Oct 16 '22 23:10

thejh


If error is thrown synchronously, express won't stop working, only returning 500.

this.app.get("/error", (request, response) => {
  throw new Error("shouldn't stop");
});

If error is thrown asynchronously, express will crash. But according to it's official documentation, there is still a way to recover from it by calling next:

this.app.get("/error", (request, response, next) => {
  setTimeout(() => {
    try {
      throw new Error("shouldn't stop");
    } catch (err) {
      next(err);
    }
  }, 0);
});

This will let express do its duty to response with a 500 error.

like image 39
xiGUAwanOU Avatar answered Oct 16 '22 22:10

xiGUAwanOU