config.error.js 979 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. const { NafError, BusinessError } = require('naf-core').Error;
  3. const { ValidationError } = require('mongoose').Error;
  4. module.exports = {
  5. json(err, ctx) {
  6. // json hander
  7. if (err instanceof BusinessError) {
  8. // 业务错误
  9. ctx.body = { errcode: err.errcode, errmsg: err.errmsg };
  10. ctx.status = 200;
  11. } else if (err instanceof NafError) {
  12. // 框架错误
  13. ctx.body = { errcode: err.errcode, errmsg: err.errmsg };
  14. ctx.status = 500;
  15. } else if (err instanceof ValidationError) {
  16. // 参数错误
  17. ctx.body = { errcode: 400, errmsg: '参数错误', details: err.message };
  18. ctx.status = 400;
  19. } else if (err instanceof Error) {
  20. // 其他错误
  21. ctx.body = { errcode: 500, errmsg: '系统错误', details: err.message };
  22. ctx.status = 500;
  23. } else {
  24. // 未知错误
  25. ctx.body = { errcode: 500, errmsg: '未知错误', details: err };
  26. ctx.status = 500;
  27. }
  28. },
  29. };