Hapi does not return data attribute from Boom error

大城市里の小女人 提交于 2020-01-03 08:39:48

问题


When replying with a Boom error from my Hapi route...

{
      method: 'PUT',
      path:'foo',
      handler: function (request, reply) {
        reply(Boom.badRequest('something', { stuff: 'and more' }));
      }
}

... I get the following response:

{"statusCode":400,"error":"Bad Request","message":"something"}

It's missing the data object which provides the details of the error! What's the deal?


回答1:


On the Hapi documentation it references the output.payload property on the boom object, set by default to include statusCode, error and message.

I was able to output the details from the boom error by setting .details on this object:

{
      method: 'PUT',
      path:'foo',
      handler: function (request, reply) {
        var err = Boom.badRequest('something', { stuff: 'and more' });
        err.output.payload.details = err.data;
        reply(err);
      }
}

Not the most ideal thing in the world, but probably a safe default.




回答2:


I had the same question, and although I can't take the approach you've taken, there is the following in the Boom FAQ:

Question How do I include extra information in my responses? output.payload is missing data, what gives?

Answer There is a reason the values passed back in the response payloads are pretty locked down. It's mostly for security and to not leak any important information back to the client. This means you will need to put in a little more effort to include extra information about your custom error. Check out the "Error transformation" section in the hapi documentation.

Also:

I found that (strangely), as the docs indicate (but not the example usage), passing a message to badImplementation is ignored, whereas passing a message to notImplemented - both are 5xx errors.

Docs for: badImplementation vs notImplemented



来源:https://stackoverflow.com/questions/27463322/hapi-does-not-return-data-attribute-from-boom-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!