哪个错误将由明示错误处理,哪个错误将由uncaughtException错误处理程序处理? [英] Which error will be handled by express error and which by uncaughtException error handler?

查看:164
本文介绍了哪个错误将由明示错误处理,哪个错误将由uncaughtException错误处理程序处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务中使用ExpressNode.js.我知道快速错误处理程序,首先使用err的回调函数

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)

我们也可以通过以下方式处理uncaughtException

And We can handle the uncaughtException also by

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

实际上,一些uncaughtException会去表达错误处理程序,而不是uncaughtException处理程序.

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

请帮助告诉我哪个错误将由express处理,哪个将由uncaughtException处理程序处理?

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

非常感谢

推荐答案

在直接从Express直接调用的函数中引发异常(throw new Error(...))时,将捕获该异常并将其转发给错误处理程序.发生这种情况是因为您的代码周围有一个try-catch块.

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.")
});

当您在未从Express直接调用的函数中引发异常(延迟或异步代码)时,没有可用的catch块来捕获此错误并正确处理它.例如,如果您有异步执行的代码:

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);
});

Express不会捕获此错误(并将其转发到错误处理程序),因为此代码周围没有包装try/catch块.在这种情况下,将触发未捕获的异常处理程序.

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.

通常,如果遇到无法恢复的错误,请使用 next(error) 正确将此错误转发到您的错误处理程序中间件:

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);

这篇关于哪个错误将由明示错误处理,哪个错误将由uncaughtException错误处理程序处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆