nodejs 同步异步异常处理封装

一、同步异常处理

//app.js
/**
 * 同步异常处理
 */

app.use(function errorHandler(err, req, res, next) {
	var accept = accepts(req);
	var type = accept.type('html', 'json', 'text');
	var jsonResult = new JsonResult();
	jsonResult.status = -1;
	jsonResult.message = "系统繁忙";
	logger.error('synchronous error on request %s %s: %s', req.method, req.url, err.message);
	if(err.domain) {
		logger.error("attention!!!, this is an unhandled error,please solve this problem as soon as possible!!!");
	}
	if(type === "json") {
		var contentType = res.get("Content-Type");
		//Content-Type为空,认为系统还没有处理过异常,这里进行处理
		if(!contentType) {
			var jsonResult = new JsonResult();
			jsonResult.status = -1;
			jsonResult.message = "系统繁忙";
			res.json(jsonResult);
		}
	} else {
		res.send(500, "系统繁忙");
	}

});

app.use(errorHandler({ dumpExceptions: true, showStack: true }));

 

二、异步异常处理

//app.js

app.use(require('express-domain-middleware'));
var domain = require('domain');
app.use(function (req, res, next) {
	//https://nodejs.org/api/domain.html#domain_domain

	//引入一个domain的中间件,将每一个请求都包裹在一个独立的domain中
	//domain来处理异步异常
	var d = domain.create();
	//监听domain的错误事件
	d.on('error', function (err) {
		logger.error('asynchronous error on request %s %s: %s', req.method, req.url, err.message);
		var contentType = res.get("Content-Type");
		//Content-Type为空,认为系统还没有处理过异常,这里进行处理
		if(!contentType) {
			var jsonResult = new JsonResult();
			jsonResult.status = -1;
			jsonResult.message = "系统繁忙";
			res.json(jsonResult);
		}
		//d.dispose();//释放一个domain对象 'domain.dispose() is deprecated
	});
	d.add(req);
	d.add(res);
	d.run(next);


});

 

三、最终的异常处理


/**
 *落网的异常最后到了这里
 */
process.on('uncaughtException', function (err) {
	console.error('serious error happend: ' + err);
});

 

四、try catch

nodejs中大部分异常都是异步的,然try catch并不能捕获异步的异常。。。

 

五、关键点

使用过promise框架的人都知道promise框架本身也可以进行异常处理监听,如果方法已经对异常进行了处理的话就没必要再公共异常处理进行异常处理了,这里通过contentType来判断各自的方法是否已经对异常进行了处理,判断的依据就是是否设置了contentType。

 

 

六、memcached 和redis操作的异常处理

   setTimeout(function () {
                    throw new Error("dsf");
                }, 100);

 

如果是memcached 和redis的异常处理,必须使用setTimeOut才能捕获到异常,这是domain的一个大坑。

 

 

 

 

 

转载于:https://my.oschina.net/fengshuzi/blog/731118

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值