覆盖环回的默认错误消息

时间:2016-10-18 20:44:25

标签: javascript node.js strongloop loopback

我试图覆盖环回的默认错误消息。这就是我的表现:

服务器/ middleware.json:

{
  "initial:before": {
    "loopback#favicon": {}
  },
  "initial": {
    "compression": {},
    "cors": {
      "params": {
        "origin": true,
        "credentials": true,
        "maxAge": 86400
      }
    },
    "helmet#xssFilter": {},
    "helmet#frameguard": {
      "params": [
        "deny"
      ]
    },
    "helmet#hsts": {
      "params": {
        "maxAge": 0,
        "includeSubdomains": true
      }
    },
    "helmet#hidePoweredBy": {},
    "helmet#ieNoOpen": {},
    "helmet#noSniff": {},
    "helmet#noCache": {
      "enabled": false
    }
  },
  "session": {},
  "auth": {},
  "parse": {},
  "routes": {
    "loopback#rest": {
      "paths": [
        "${restApiRoot}"
      ]
    }
  },
  "files": {},
  "final": {
    "loopback#urlNotFound": {}
  },
  "final:after": {
    "loopback#errorHandler": {},
    "errorHandler": {}
  }
}

服务器/中间件/错误handler.js:

module.exports = (error, req, res, next) => {
  console.log('a')
};

在覆盖之前,我需要先拦截错误信息,但我没有得到如何......

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以访问错误消息并将其覆盖如下

module.exports = (error, req, res, next) => {
  console.log(error.message); // logs out original error message
  error.message = 'Your Custom Error message'; // this will overwrite the error message
  next(error); // this is important 
};

错误对象的其他属性是

statusCode, name, stack & also details, messages if applicable