Node.js中出現(xiàn)未捕獲異常的處理方法
前言
Node.js 程序運行在單進程上,應(yīng)用開發(fā)時一個難免遇到的問題就是異常處理,對于一些未捕獲的異常處理起來,也不是一件容易的事情。
未捕獲異常的程序
下面展示了一段簡單的應(yīng)用程序,如下所示:
const http = require('http');
const PORT = 3000;
const server = http.createServer((req, res) => {
if (req.url === '/error') {
a.b;
res.end('error');
} else {
setTimeout(() => res.end('ok!'), 1000 * 10);
}
});
server.listen(PORT, () => console.log(`port is listening on ${PORT}.`));
運行以上程序,在右側(cè)第二個窗口中執(zhí)行了 /error 路由,因為沒有定義 a 這個對象,則會引發(fā)錯誤。

進程崩潰退出之后導(dǎo)致整個應(yīng)用程序也將崩潰,左側(cè)是一個延遲的響應(yīng),也將無法正常工作。
這是一個頭疼的問題,不要緊,下文我們將會學到一個優(yōu)雅退出的方案。
進程崩潰優(yōu)雅退出
關(guān)于錯誤捕獲,Node.js 官網(wǎng)曾提供了一個模塊 domain 來實現(xiàn),但是現(xiàn)在已廢棄了所以就不再考慮了。
之前在看 CNPM 這個項目時看到了以下關(guān)于錯誤退出的一段代碼:
// https://github.com/cnpm/cnpmjs.org/blob/master/worker.js#L18
graceful({
server: [registry, web],
error: function (err, throwErrorCount) {
if (err.message) {
err.message += ' (uncaughtException throw ' + throwErrorCount + ' times on pid:' + process.pid + ')';
}
console.error(err);
console.error(err.stack);
logger.error(err);
}
});
上述使用的是 graceful 這個模塊,在 NPM 上可以找到。
實現(xiàn)一個 graceful.js
實現(xiàn)一個 graceful 函數(shù),初始化加載時注冊 uncaughtException、unhandledRejection 兩個錯誤事件,分別監(jiān)聽未捕獲的錯誤信息和未捕獲的 Promise 錯誤信息。
const http = require('http');
/**
* graceful
* @param { Number } options.killTimeout 超時時間
* @param { Function } options.onError 產(chǎn)生錯誤信息會執(zhí)行該回調(diào)函數(shù)
* @param { Array } options.servers Http Server
* @returns
*/
function graceful(options = {}) {
options.killTimeout = options.killTimeout || 1000 * 30;
options.onError = options.onError || function () {};
options.servers= options.servers || [];
process.on('uncaughtException', error => handleUncaughtException(error, options));
process.on('unhandledRejection', error => handleUnhandledRejection(error, options));
}
handleUncaughtException、handleUnhandledRejection 分別接收相應(yīng)的錯誤事件,執(zhí)行應(yīng)用傳入的 onError() 將錯誤信息進行回傳,最后調(diào)用 handleError()。
const throwCount = {
uncaughtException: 0,
unhandledRejection: 0
};
function handleUncaughtException(error, options) {
throwCount.uncaughtException += 1;
options.onError(error, 'uncaughtException', throwCount.uncaughtException);
if (throwCount.uncaughtException > 1) return;
handleError(options);
};
function handleUnhandledRejection(error, options) {
throwCount.unhandledRejection += 1;
options.onError(error, 'unhandledRejection', throwCount.unhandledRejection);
if (throwCount.unhandledRejection > 1) return;
handleError(options);
}
HandleError 方法為核心實現(xiàn),首先遍歷應(yīng)用傳入的 servers,監(jiān)聽 request 事件,在未捕獲錯誤觸發(fā)之后,如果還有請求鏈接,則關(guān)閉當前請求的鏈接。
之后,執(zhí)行 setTimeout 延遲退出,也就是最大可能的等待之前鏈接處理完成。
function handleError(options) {
const { servers, killTimeout } = options;
// 關(guān)閉當前請求的鏈接
for (const server of servers) {
console.log('server instanceof http.Server: ', server instanceof http.Server);
if (server instanceof http.Server) {
server.on('request', (req, res) => {
req.shouldKeepAlive = false;
res.shouldKeepAlive = false;
if (!res._header) {
res.setHeader('Connection', 'close');
}
});
}
}
// 延遲退出
const timer = setTimeout(() => {
process.exit(1);
}, killTimeout);
if (timer && timer.unref) {
timer.unref();
}
}
module.exports = graceful;
應(yīng)用程序中使用上述實現(xiàn)
加載上述 graceful.js 使用起來很簡單只需要在文件尾部,加載 graceful 函數(shù)并傳入相應(yīng)參數(shù)即可。
const graceful = require('./graceful.js');
...
server.listen(PORT, () => console.log(`port is listening on ${PORT}.`));
graceful({
servers: [server],
onError: (error, type, throwErrorCount) => {
console.log('[%s] [pid: %s] [throwErrorCount: %s] %s: %s', new Date(), process.pid, throwErrorCount, type, error.stack || error);
}
});
再次運行應(yīng)用程序,看看效果:

這一次,即使右側(cè) /error 路由產(chǎn)生未捕獲異常,也將不會引起左側(cè)請求無法正常響應(yīng)。
Graceful 模塊
最后推薦一個 NPM 模塊 graceful,引用文檔中的一句話:“It's the best way to handle uncaughtException on current situations.”
該模塊還提供了對于 Node.js 中 Cluster 模塊的支持。
安裝
$ npm install graceful -S
應(yīng)用
如果一個進程中有多個 Server,將它們添加到 servers 中即可。
const graceful = require('graceful');
...
graceful({
servers: [server1, server2, restapi],
killTimeout: '15s',
});
總結(jié)
如果你正在使用 Node.js 對于異常你需要有些了解,上述講解的兩個異常事件可以做為你的最后補救措施,但是不應(yīng)該當作 On Error Resume Next(出了錯誤就恢復(fù)讓它繼續(xù))的等價機制。
到此這篇關(guān)于Node.js中出現(xiàn)未捕獲異常處理方法的文章就介紹到這了,更多相關(guān)Node.js未捕獲異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
Reference
- nodejs.cn/api/process.html
- www.npmjs.com/package/graceful
相關(guān)文章
node版本太高導(dǎo)致項目跑不起來的解決辦法(windows)
換了臺電腦后,安裝node,一切完美,發(fā)現(xiàn)其中有一個uniapp的小程序項目跑不起來,感覺是node版本太高導(dǎo)致的,所以只能重新安裝低版本的node,本文給大家介紹了node版本太高的解決辦法,需要的朋友可以參考下2023-10-10
win系統(tǒng)下nodejs環(huán)境安裝配置
這篇文章主要介紹了win系統(tǒng)下nodejs環(huán)境安裝配置的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
如何刪除所有node_modules和package-lock配置文件
這篇文章主要介紹了如何刪除所有node_modules和package-lock配置文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Node 創(chuàng)建第一個服務(wù)器應(yīng)用的操作方法
Node.js是一個基于Chrome V8引擎的JavaScript運行環(huán)境,可以用于構(gòu)建高性能的網(wǎng)絡(luò)應(yīng)用程序,它采用事件驅(qū)動、非阻塞I/O模型,使得程序可以以高效地方式處理并發(fā)請求,這篇文章主要介紹了Node 創(chuàng)建第一個服務(wù)器應(yīng)用,需要的朋友可以參考下2024-02-02

