error_handler.js 394 B

12345678910111213141516171819
  1. 'use strict';
  2. module.exports = () => {
  3. return async function errorHandler(ctx, next) {
  4. try {
  5. await next();
  6. } catch (err) {
  7. const { message } = err;
  8. let json = {
  9. errcode: -1001,
  10. errmsg: message,
  11. };
  12. if (err.code !== 'ERR_ASSERTION') {
  13. json = JSON.parse(message);
  14. }
  15. ctx.body = json;
  16. ctx.status = 400;
  17. }
  18. };
  19. };