Which error will be handled by express error and which by uncaughtException error handler?

一个人想着一个人 提交于 2019-12-11 13:46:57

问题


In my services using express nodejs. I knew about the express error handler, the callback function with err first

app.use(function(err, req, res, next)

And We can handle the uncaughtException also by

process.on('uncaughtException', function(err) {}

In fact, some uncaughtException will go to express error handler not uncaughtException handler.

Please help to tell me which error will be handled by express, which by uncaughtException handler?

Many thanks


回答1:


When you throw an exception (throw new Error(...)) in a function that was directly called from Express, it will be catched and forwarded it to your error handler. This happens because there's a try-catch block around your code.

app.get("/throw", function(request, response, next) {
    throw new Error("Error thrown in scope of this function. Express.js will delegate it to error handler.")
});

When you throw an exception in a function that is not directly called from Express (deferred or async code), there's no catch block available to catch this error and handle it properly. For example, if you have code that gets executed asynchronously:

app.get("/uncaught", function(request, response, next) {
    // Simulate async callback using setTimeout.
    // (Querying the database for example).
    setTimeout(function() {
        throw new Error("Error thrown without surrounding try/catch. Express.js cannot catch it.");
    }, 250);
});

This error won't be catched by Express (and forwarded to the error handler) because there's no wrapping try/catch block around this code. In this case, the uncaught exception handler will be triggered instead.

In general, if you encounter an error from which you cannot recover, use next(error) to properly forward this error to your error handler middleware:

app.get("/next", function(request, response, next) {
    // Simulate async callback using setTimeout.
    // (Querying the database for example).
    setTimeout(function() {
        next(new Error("Error always forwarded to the error handler."));
    }, 250);
});

Below is a full example to play around with:

var express = require('express');

var app = express();

app.get("/throw", function(request, response, next) {
    throw new Error("Error thrown in scope of this function. Express.js will delegate it to error handler.")
});

app.get("/uncaught", function(request, response, next) {
    // Simulate async callback using setTimeout.
    // (Querying the database for example).
    setTimeout(function() {
        throw new Error("Error thrown without surrounding try/catch. Express.js cannot catch it.");
    }, 250);
});

app.get("/next", function(request, response, next) {
    // Simulate async callback using setTimeout.
    // (Querying the database for example).
    setTimeout(function() {
        next(new Error("Error always forwarded to the error handler."));
    }, 250);
});


app.use(function(error, request, response, next) {
    console.log("Error handler: ", error);
});

process.on("uncaughtException", function(error) {
    console.log("Uncaught exception: ", error);
});

// Starting the web server
app.listen(3000);


来源:https://stackoverflow.com/questions/33541217/which-error-will-be-handled-by-express-error-and-which-by-uncaughtexception-erro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!