error_handler.js 491 B

1234567891011121314151617181920
  1. 'use strict';
  2. module.exports = () => {
  3. return async function errorHandler(ctx, next) {
  4. try {
  5. await next();
  6. } catch (err) {
  7. console.log(err);
  8. if (err.status === 401) {
  9. ctx.body = { errcode: err.status, errmsg: '请登录' };
  10. ctx.status = err.status;
  11. return false;
  12. }
  13. const { message } = err;
  14. const json = JSON.parse(message);
  15. ctx.logger.error(json.error);
  16. ctx.body = json;
  17. ctx.status = 400;
  18. }
  19. };
  20. };