简体   繁体   English

环回自定义远程方法错误回调

[英]Loopback customize remote method error callback

I have a little problems with loopback models. 我对环回模型有一些问题。 I have various models with remote method attacked. 我有各种受到远程方法攻击的模型。 All methods respond with own callback function. 所有方法都使用自己的回调函数进行响应。 My problem start when I put error object in the callback function. 当我在回调函数中放置错误对象时,我的问题就开始了。 For example: 例如:

 promise()
        .then(promiseResult => sencondPromise())
        .then(promiseResult => cb(null, promiseResult))
        .catch(err => cb({"status" : 400, "message" : "Response test"}, null));

There aren't problems when triggered positive callback. 触发正回调时没有问题。 But when triggered negative callback: 但是当触发否定回调时:

cb({"status" : 400, "message" : "Response test"}, null));

I get this response: 我得到这个回应:

{
  "error": {
    "statusCode": 400,
    "message": "Response test"
  }
}

Why status field it's changed to statusCode? 为什么将状态字段更改为statusCode?

Thanks in advance. 提前致谢。

For creating your own error handler : 创建自己的错误处理程序:

1) Remove strong-error-handler from middleware.json 1)从middleware.json删除strong-error-handler

2) Create config.local.js and put below code there: 2)创建config.local.js并在其中放置以下代码:

function errorConverter(options){
  return function(err, req, res, next){
    //check err and create your custom error object
    var customErr = {};
    next(customErr);
  }
}

module.exports = {
  remoting: {
    errorHandler: {
      handler: errorConverter()
    }
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM