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

Add data property to the response object #49

Closed
SzaboAdamImre opened this issue Apr 7, 2015 · 4 comments
Closed

Add data property to the response object #49

SzaboAdamImre opened this issue Apr 7, 2015 · 4 comments
Labels
non issue Issue is not a problem or requires changes

Comments

@SzaboAdamImre
Copy link

Hello!

Most of the Boom methods get an additional (optional) data property, what Hapi doesn't send forward in the response.
I tried using the Hapi response/tail events, but couldn't find the given data in the inputs of those events.

To add the data property:

var err = require('boom').create(451, "Msg", {name: "name"});
err.output.payload.data = err.data;
res(err);

Which is a bit more code than it is needed to write:

res(require('boom').create(451, "Msg", {name: "name"}));

The solution proposed in #47 works for me as well (as well as the data property could be used instead of the solution proposed there).

The use case is the following:
I want to send error messages, which messages should be translated on the frontend. So the message would be like an error code, and the parameters for that code would be in the data object
So a real example could be the following:

res(require('boom').create(422, "ERRORS.MUST_BE_SET", {entityName: "user", fields: ['name','email']}));

And the response for this would be preferred:

{
    "statusCode": 422,
    "error": "Unprocessable Entity",
    "message": "ERRORS.MUST_BE_SET",
    "data":  {
        "entityName": "user",
        "fields": ["name","email"]
    }
}

Thx
Adam

ps.: I might be not aware of some functionality in hapi/boom, if this is the case, pls point the existing functionality out for me

@SzaboAdamImre
Copy link
Author

Found it:
For future reference, if somebody needs it:

    server.ext('onPreResponse', function (request, reply) {
        var response = request.response;
        if (!response.isBoom) {
            return reply.continue();
        }
        if (response.data){
            response.output.payload.data = response.data;
        }
        return reply(response);
    });

This code will run for all responses, and if the response is a boom object, and the data property was set on the response, it puts it on the final response object.

@phuonghuynh
Copy link

This is not support in native way?

@sethlivingston
Copy link

sethlivingston commented Mar 19, 2018

More recent solution:

server.ext('onPreResponse', (request, h) => {
  const response = request.response;
  if (!response.isBoom)
    h.continue;

  const is4xx = response.output.statusCode >= 400 && response.output.statusCode < 500;
  if (is4xx && response.data)
    response.output.payload.data = response.data;

  return h.continue;
});

@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
non issue Issue is not a problem or requires changes
Projects
None yet
Development

No branches or pull requests

4 participants