捕获错误,但不退出App - NodeJS

时间:2018-05-30 17:59:37

标签: javascript node.js express

我是NodeJS的新手。我试图

  • 抓住导致错误的不良案例
  • 当我抓住它时,我想在我的控制台中抛出错误
  • 防止执行更多代码
  • 防止应用崩溃

#1

  

我已尝试 process.exit

我让应用程序退出,我不想要

[nodemon] clean exit - waiting for changes before restart

#2

  

我也试过 process.abort

我让应用程序崩溃,我不想要。

[nodemon] app crashed - waiting for file changes before starting...

#3

  

我也试过 uncaughtException

if(serviceType == 0 || serviceType == '' || serviceType == null) {
    console.error('\x1b[41m%s\x1b[0m', 'bad service type detected');
    process.on('uncaughtException', function (err) {
      console.error(JSON.parse(JSON.stringify(body)));
    });
}

我有控制台打印但是代码仍然保持不变,我不想要

#4

  

我还尝试了 throw new Error

if(serviceType == 0 || serviceType == '' || serviceType == null) {
    console.error('\x1b[41m%s\x1b[0m', 'bad service type detected');
    throw new Error(serviceType);
}

我有控制台打印,我得到代码停止,但我在控制台中遇到了大错误

2018-05-30T17:45:37.307Z - info: The server is listening on port 3333
bad service type detected
{ mac: '102030405060' }
Error: 1
    at Promise (/Users/john/Sites/app/server/factories/policy-factory.js:23:27)
    at Promise._execute (/Users/john/Sites/app/node_modules/bluebird/js/release/debuggability.js:303:9)
    at Promise._resolveFromExecutor (/Users/john/Sites/app/node_modules/bluebird/js/release/promise.js:483:18)
    at new Promise (/Users/john/Sites/app/node_modules/bluebird/js/release/promise.js:79:10)
    at Object.policyFactory.create (/Users/john/Sites/app/server/factories/policy-factory.js:13:16)
    at fortinet.createPolicy (/Users/john/Sites/app/server/controllers/fortinet.js:44:47)
    at Layer.handle [as handle_request] (/Users/john/Sites/app/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/john/Sites/app/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/john/Sites/app/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/john/Sites/app/node_modules/express/lib/router/layer.js:95:5)
    at /Users/john/Sites/app/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/john/Sites/app/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/john/Sites/app/node_modules/express/lib/router/index.js:275:10)
    at urlencodedParser (/Users/john/Sites/app/node_modules/body-parser/lib/types/urlencoded.js:82:7)
    at Layer.handle [as handle_request] (/Users/john/Sites/app/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/john/Sites/app/node_modules/express/lib/router/index.js:317:13)
    at /Users/john/Sites/app/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/john/Sites/app/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/john/Sites/app/node_modules/express/lib/router/index.js:275:10)
    at /Users/john/Sites/app/node_modules/body-parser/lib/read.js:130:5
    at invokeCallback (/Users/john/Sites/app/node_modules/raw-body/index.js:224:16)
    at done (/Users/john/Sites/app/node_modules/raw-body/index.js:213:7)
2018-05-30T17:45:46.817Z - error: POST /eventserver/ext/vcpe - 1
2018-05-30T17:45:46.818Z - debug: {"body":{"mac":"102030405060"}}
2018-05-30T17:45:46.818Z - verbose: {"token":""}

#5

  

我也试过 return;

if(serviceType == 0 || serviceType == '' || serviceType == null) {
    console.error('\x1b[41m%s\x1b[0m', 'bad service type detected');
    return;
}

我让应用程序没有崩溃,并让代码停止它们的位置。

但我请求的过程似乎挂了这么久。

我不确定这是否是最佳方法,但到目前为止,这种方法似乎达到了我想要做的很多方面。

问题

如何进行并实现这样的目标?

我现在对任何建议持开放态度。

2 个答案:

答案 0 :(得分:1)

我可能会使用try和catch块扩展#4:



// Used for the snippet only
const serviceType = 0;

try {
  // Some logic

  if(serviceType == 0 || serviceType == '' || serviceType == null) {
      throw new Error('Bad service type detected!');
  }
  
  console.log('This message doesn\'t print the console!');

  // Some more logic
} catch (e) {
  console.error(e.message);
}




这样你的程序就不会在try块中继续,你可以在catch块中以任何你喜欢的方式处理错误。

答案 1 :(得分:1)

如果您使用Expressjs,您可以按照Expressjs网站中的建议定义错误处理中间件。由于大多数使用的Node框架是表达的,我希望这可能有所帮助。

app.use(function (err, req, res, next) {
  console.error(err.stack)
  res.status(500).send('Something broke!')
})

这将捕获服务器端错误并发送消息而不会导致应用程序崩溃。官方链接https://expressjs.com/en/guide/error-handling.html