如何通过实例分析掌握node.js express捕获全局异常的三种方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计567个文字,预计阅读时间需要3分钟。
原文示例:本文实例讲述了node.js+express捕获全局异常的三种方法。分享给广大家长,供大家参考,具体如下:
+ 场景:express的路由中抛出异常后,全局中间件没有捕获方法,需要在所有的路由函数中写try+catch。+ 方法:在express中,可以通过以下三种方式捕获全局异常:
1. 使用全局错误处理中间件
2.使用错误事件监听
3.使用Promise的catch方法
具体实现如下:
1. 使用全局错误处理中间件
javascript
app.use((err, req, res, next)=> { console.error(err.stack); res.status(500).send('Something broke!');});2. 使用错误事件监听
javascriptprocess.on('uncaughtException', (err)=> { console.error('Uncaught exception:', err); process.exit(1);});
process.on('unhandledRejection', (reason, promise)=> { console.error('Unhandled rejection at:', promise, 'reason:', reason); process.exit(1);});
3. 使用Promise的catch方法
javascriptnew Promise((resolve, reject)=> { // 模拟异步操作 setTimeout(()=> { throw new Error('异步操作出错'); }, 1000);}).then(()=> { console.log('成功');}).catch((err)=> { console.error('Promise错误:', err);});
本文实例讲述了node.js express捕获全局异常的三种方法。分享给大家供大家参考,具体如下:
场景
express的路由里抛出异常后,全局中间件没办法捕获,需要在所有的路由函数里写try catch,这坑爹的逻辑让人每次都要多写n行代码
官方错误捕获中件间代码如下
app.use(function(err, req, res, next) { console.error(err.stack); res.status(500).send('Something broke!'); });
测试证明客户端已经卡死,没有返回结果
解决方法一
process.on('uncaughtException', function(err) { console.log('Caught exception: ' + err); });
虽然可以捕获,在命令行有输出,但是没办法给客户端返回错误了
解决方法二
const Layer = require('express/lib/router/layer'); Object.defineProperty(Layer.prototype, 'handle', { enumerable: true, get() { return this.__handle; }, set(fn) { if (fn.length === 4) { this.__handle = fn; } else { this.__handle = (req, res, next) => Promise.resolve() .then(() => fn(req, res, next)) .catch(next); } }, });
解决方法三
安装express-async-errors,没错,已经有人受不了express不能捕获Promise异常搞了个破解包
地址github.com/davidbanham/express-async-errors
npm install express-async-errors --save
使用
var express = require('express'); require('express-async-errors');
希望本文所述对大家node.js程序设计有所帮助。
本文共计567个文字,预计阅读时间需要3分钟。
原文示例:本文实例讲述了node.js+express捕获全局异常的三种方法。分享给广大家长,供大家参考,具体如下:
+ 场景:express的路由中抛出异常后,全局中间件没有捕获方法,需要在所有的路由函数中写try+catch。+ 方法:在express中,可以通过以下三种方式捕获全局异常:
1. 使用全局错误处理中间件
2.使用错误事件监听
3.使用Promise的catch方法
具体实现如下:
1. 使用全局错误处理中间件
javascript
app.use((err, req, res, next)=> { console.error(err.stack); res.status(500).send('Something broke!');});2. 使用错误事件监听
javascriptprocess.on('uncaughtException', (err)=> { console.error('Uncaught exception:', err); process.exit(1);});
process.on('unhandledRejection', (reason, promise)=> { console.error('Unhandled rejection at:', promise, 'reason:', reason); process.exit(1);});
3. 使用Promise的catch方法
javascriptnew Promise((resolve, reject)=> { // 模拟异步操作 setTimeout(()=> { throw new Error('异步操作出错'); }, 1000);}).then(()=> { console.log('成功');}).catch((err)=> { console.error('Promise错误:', err);});
本文实例讲述了node.js express捕获全局异常的三种方法。分享给大家供大家参考,具体如下:
场景
express的路由里抛出异常后,全局中间件没办法捕获,需要在所有的路由函数里写try catch,这坑爹的逻辑让人每次都要多写n行代码
官方错误捕获中件间代码如下
app.use(function(err, req, res, next) { console.error(err.stack); res.status(500).send('Something broke!'); });
测试证明客户端已经卡死,没有返回结果
解决方法一
process.on('uncaughtException', function(err) { console.log('Caught exception: ' + err); });
虽然可以捕获,在命令行有输出,但是没办法给客户端返回错误了
解决方法二
const Layer = require('express/lib/router/layer'); Object.defineProperty(Layer.prototype, 'handle', { enumerable: true, get() { return this.__handle; }, set(fn) { if (fn.length === 4) { this.__handle = fn; } else { this.__handle = (req, res, next) => Promise.resolve() .then(() => fn(req, res, next)) .catch(next); } }, });
解决方法三
安装express-async-errors,没错,已经有人受不了express不能捕获Promise异常搞了个破解包
地址github.com/davidbanham/express-async-errors
npm install express-async-errors --save
使用
var express = require('express'); require('express-async-errors');
希望本文所述对大家node.js程序设计有所帮助。

