Review
- 2023-10-10 22:36
- 2024-10-03
一、Introduction #
Error handling is a way to find bugs and solve them as quickly as humanly possible. The errors in Node.js can be either operation or programmer errors.
有两种错误
- 编程错误:程序员写出的错误(语法错误、引用错误、类型错误)
- 业务错误:外部因素导致服务不能正常运行(超时、服务端异常、输入错误)
Error handling techniques #
throwtry…catchblocks- Callbacks
- Promises or Async/await
- Event emitters(events)
- Middleware
unhandledRejection,uncaughtException,uncaughtExceptionMonitor
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});process.on('unhandledRejection', (err) => {
})Common exit codes in Node.js:
- 0: The program exited normally.
- 1: A generic error occurred.
- 12: The program received a SIGTERM signal (termination signal).
- 13: The program received a SIGKILL signal (kill signal).
- 14: The program received a SIGALRM signal (alarm signal).
- 15: The program received a SIGTERM signal and had a timeout.
Other possible exit codes:
- 2: A syntax error occurred.
- 3: A runtime error occurred.
- 4: A system error occurred.
- 5: A disk full error occurred.
- 6: An unknown error occurred.