error-handler.js 755 Bytes
/**
 * error处理
 * @author: leo<qi.li@yoho.cn>
 * @date: 2017/06/23
 */
const logger = global.yoho.logger;

const serverError = (err, req, res, next) => { // eslint-disable-line
  logger.error(err);
  let code = 500, msg = '服务器错误!';

  if (err.code && typeof err.code === 'number' && err.code !== 500 && err.message && !/_ERROR/.test(err.message)) {
    code = err.code;
    msg = err.message;
  }
  return res.status(code).json({
    code: code,
    message: msg
  });
};


const notFound = (req, res) => {
  res.status(404);

  if (req.xhr) {
    return res.json({
      code: 404,
      message: '请求路径不存在'
    });
  }

  res.render('error/404', {
    layout: false
  });
};

module.exports = {
  notFound,
  serverError
};