Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change to response object in afterError hook not formatted as expected #3269

Closed
ghost opened this issue Mar 10, 2017 · 1 comment
Closed

Comments

@ghost
Copy link

ghost commented Mar 10, 2017

In my hook.js file I have:

remotes.afterError('**', function(ctx, next, method){
        ctx.error = {
            code: ctx.error.statusCode,
            statusCode: ctx.error.statusCode,
            error: true,
            errors: [{
                message: ctx.error.message
            }],
            data: []
        };
        return next(ctx.error);
    });

My response object looks like this:

{
  "error": {
    "statusCode": 401,
    "code": 401,
    "error": true,
    "errors": [
      {
        "message": "login failed"
      }
    ],
    "data": []
  }
}

Why does it NOT look like this:

{
    "statusCode": 401,
    "code": 401,
    "error": true,
    "errors": [
      {
        "message": "login failed"
      }
    ],
    "data": []
  }

Which is the same thing minus the wrapping in an error object.

@bajtos
Copy link
Member

bajtos commented Mar 13, 2017

The error response format is controller by error-handling middleware, which is typically strong-error-handler in LoopBack 3.x and recently scaffolded LoopBack 2.x applications.

You can replace strong-error-handler with your own middleware to control the format of JSON responses.

We had considered the ability to configure the format of HTTP responses in strong-error-handler too, but didn't have bandwidth to implement it yet - see loopbackio/strong-error-handler#8. Contributions welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant