error_handler.js 696 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. module.exports = () => {
  3. return async function errorHandler(ctx, next) {
  4. const { method } = ctx.request;
  5. try {
  6. await next();
  7. const jsons = await ctx.service.log.init();
  8. if (method !== 'GET' && jsons) {
  9. jsons.result = '成功';
  10. await ctx.service.log.create(jsons);
  11. }
  12. } catch (err) {
  13. const jsons = await ctx.service.log.init();
  14. if (method !== 'GET' && jsons) {
  15. jsons.result = '失败';
  16. await ctx.service.log.create(jsons);
  17. }
  18. const { message } = err;
  19. const json = {
  20. errcode: -1001,
  21. errmsg: message,
  22. };
  23. ctx.body = json;
  24. ctx.status = 400;
  25. }
  26. };
  27. };