All files / koa/middleware error.js

94.44% Statements 17/18
64.29% Branches 9/14
100% Functions 2/2
94.44% Lines 17/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39    5x   5x 5x 88x 88x   23x 17x   6x   23x               23x 23x 23x 23x 23x           23x 23x         5x
'use strict'
// const _ = require('lodash')
const logger = require('../util/logger').tag('error')
 
const errorHandler = function () {
  return async(ctx, next) => {
    try {
      await next()
    } catch (err) {
      if (err.status && err.status < 500) {
        logger.warn(err.stack || err);
      } else {
        logger.error(err.stack || err);
      }
      ctx.status = err.status || 500
      // if (err.errors && typeof(err.errors) === 'object') {
      //   _.mapValues(err.errors, (item) => {
      //     if (item.message) {
      //       msg = item.message
      //     }
      //   })
      // }
      let msg = 'Internal Server Error'
      Eif (err.message) {
        Eif (err.message.indexOf(';;')) {
          let msgs = err.message.split(';;');
          msg = msgs[0];
          // code = msgs[1];
        } else {
          msg = JSON.stringify(err.message);
        }
      }
      ctx.body = { msg: msg }
      ctx.app.emit('error', err, ctx)
    }
  }
}
 
module.exports = errorHandler