Antonio
Antonio

Reputation: 55

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?

Thanks in advance.

Upvotes: 0

Views: 1134

Answers (1)

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9406

For creating your own error handler :

1) Remove strong-error-handler from middleware.json

2) Create config.local.js and put below code there:

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()
    }
  }
};

Upvotes: 2

Related Questions