首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用强错误处理程序截获环回应用程序中的所有>= 500错误代码

使用强错误处理程序截获环回应用程序中的所有>= 500错误代码
EN

Stack Overflow用户
提问于 2019-09-11 12:32:30
回答 1查看 314关注 0票数 0

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

代码语言:javascript
复制
"final:after": {
    "./middlewares/log-error": {},
    "strong-error-handler": {
      "params": {
        "debug": false,
        "log": true
      }
    }
  }

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

代码语言:javascript
复制
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);
    };
  }

我有一个简单的函数,如下所示

代码语言:javascript
复制
function myfunc(){
   myvar.asdf #myvar is undefined
}

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

我想避免应用程序崩溃。一个简单的try/catch有助于避免这个错误,但是有没有一种方法可以避免在发生这样的错误时节点崩溃呢?

EN

回答 1

Stack Overflow用户

发布于 2019-09-11 13:14:37

使用uncaughtException,因为您的应用程序不会崩溃,并且您可以选择如何处理错误。

代码语言:javascript
复制
process.on('uncaughtException', function (err) {
  console.log(err);
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57882193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档