【问题标题】:Intercepting all >= 500 error codes in a loopback application with strong-error-handler使用强错误处理程序在环回应用程序中拦截所有 >= 500 个错误代码
【发布时间】:2019-09-11 13:14:36
【问题描述】:

我有一个简单的loopback.js 应用程序,其中包含strong-error-handler https://github.com/strongloop/strong-error-handlermiddlewares/middleware.json 中的以下代码

"final:after": {
    "./middlewares/log-error": {},
    "strong-error-handler": {
      "params": {
        "debug": false,
        "log": true
      }
    }
  }

我的middlewares/log-error 文件如下所示

module.exports = function createErrorLogger(options) {
    return function logError(err, req, res, next) {
      // your custom error-logging logic goes here
        console.log("inside custom error logging");
      const status = err.status || err.statusCode;
      if (status >= 500) {
        // log only Internal Server errors
        console.log('Unhandled error for request %s %s: %s',
          req.method, req.url, err.stack || err);
      }

      // Let the next error handler middleware
      // produce the HTTP response
      next(err);
    };
  }

我有一个像这样的简单函数

function myfunc(){
   myvar.asdf #myvar is undefined
}

在上述函数中myvar 是未定义的。所以当我调用这个函数时 http://localhost:3001/myfunc 它抛出这个错误并且应用程序崩溃

我想避免应用崩溃。一个简单的try/catch 有助于避免这个错误,但是有没有办法避免发生这种错误时节点崩溃?

【问题讨论】:

    标签: node.js loopbackjs strongloop


    【解决方案1】:

    使用 uncaughtException,因为您的应用程序不会崩溃,并且您可以选择要对错误执行的操作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2019-04-06
      • 2018-07-26
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      相关资源
      最近更新 更多