1

I have inherited some very messy loopback code and I want to know if it is possible to return a status code other than 200.

For example I have the following code:

  PensionsUser.loginView = function (ctx, credentials, cb) {
    var respJSON = util.getresponseTemplate();

    credentials.email = credentials.email.toLowerCase().trim();
    PensionsUser.login(credentials, 'user', function (err, loginResp) {
      if (err) {
        util.addErrorToResponse(err, respJSON, 'NO-PUS-LV-001');
        app.log.error(util.loggingTemplate('PensionsUser', 'loginView', 'NO-PUS-LV-001', credentials.email));
        ctx.res.status = 401;  //does not work
        cb(null, respJSON);
     //etc.

I know cb(null, respJSON) should be returning the error like this cb(respJSON) but sadly the frontend code relies on this JSON being returned as currently is so my first step would be to just change the status code.

Is this possible?

dagda1
  • 26,856
  • 59
  • 237
  • 450

3 Answers3

2

You can set custom error like {error:500, message: "Custom Error"} or you can configure "strong-error-handler" in middleware config file.

J.Doe
  • 35
  • 7
0

For error you can set status like this:

callback({status: 401, message: 'Your error message'});

And for success status codes you someone already answered here: https://stackoverflow.com/a/26942431/1827886

Community
  • 1
  • 1
Mantas
  • 71
  • 7
0

To send response code according to your own requirements, you can use this code:

 "YourModelName"."YourRemoteMethod" = function("YourAcceptArguments", callback) {

      var error = new Error("New password and confirmation do not match");
      error.status = "Enter the resposne code that you want to send";
      // Example => error.status = 400 
      return callback(error);
    };
Irtiza
  • 173
  • 4
  • 16