如何预防你的node.js程序崩溃(译)

2018-09-21  本文已影响0人  ITgecko

第一个问题 未处理的rejections

(node:48446) UnhandledPromiseRejectionWarning: This is fine
(node:48446) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:48446) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', (reason, promise) => {
  console.log('Unhandled Rejection:', reason)
  // 在这里处理
})
window.addEventListener('unhandledrejection, '({
    reason
  }) => {
   // 在这里处理错误
  });

第二个问题 steam中未处理的错误事件

const fs = require('fs')
const stream = fs.createReadStream('does-not-exist.txt')
events.js:137
      throw er; // Unhandled 'error' event
      ^
Error: ENOENT: no such file or directory, open 'do-not-exists.txt'

额外的炸弹: JSON.parse()

app.get('/foo', (req, res, next) => {
  const foo = JSON.parse(req.body.jsonString)
  // ...
})
上一篇下一篇

猜你喜欢

热点阅读