error_handler.js 654 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. /**
  3. * 异常处理
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. module.exports = () => {
  10. return function* errorHandler(next) {
  11. try {
  12. yield next;
  13. } catch (err) {
  14. // 注意:自定义的错误统一处理函数捕捉到错误后也要 `app.emit('error', err, this)`
  15. // 框架会统一监听,并打印对应的错误日志
  16. this.app.emit('error', err, this);
  17. // mysql重连
  18. if (err.code === 'PROTOCOL_CONNECTION_LOST') {
  19. this.app.mysql.createInstance(this.app.config.mysql.client);
  20. }
  21. }
  22. };
  23. };