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

Support failAction function on validation response failures #3214

Closed
alexspence opened this issue Jun 21, 2016 · 4 comments
Closed

Support failAction function on validation response failures #3214

alexspence opened this issue Jun 21, 2016 · 4 comments
Assignees
Labels
Milestone

Comments

@alexspence
Copy link

@alexspence alexspence commented Jun 21, 2016

I can configure a custom handler to handle validation failures for request validation. It would be nice to be able to do the same for response validation. Specifically, we need the ability to add a requestId header to help with issue reports.

Let me know if you want a PR.

@hueniverse
Copy link
Contributor

@hueniverse hueniverse commented Jun 21, 2016

I need more info. Can you show a mock example?

alexspence added a commit to alexspence/hapi that referenced this issue Jun 22, 2016
alexspence added a commit to alexspence/hapi that referenced this issue Jun 22, 2016
@alexspence
Copy link
Author

@alexspence alexspence commented Jun 22, 2016

We use request.id to log actions of this request throughout the lifecycle. Our code looks something like this.



server.ext('onPreResponse', (request, reply) => {
  request.response.header('x-request-id', request.id);  // set at the beginning of the request lifecycle.
})

const route = {
  method: 'GET',
  path: '/products/{id}/most_helpful',
  config: {
    handler: reviewController.mostHelpfulByProduct,
    description: 'List of Most Helpful Reviews',
    validate: {
      params: Joi.object().keys({
        id: reviewDef.params.productId().required()
      }),
      query: Joi.object().keys({
        perpage: reviewDef.params.perpage().default(5),
        positiverating: reviewDef.params.positiveRating(),
        negativerating: reviewDef.params.negativeRating(),
        threshold: reviewDef.params.threshold(),
        expand: reviewDef.params.expand()
      })
      failAction = (request, reply, source, error) => {
        return responseDecorator.sendError(request.context, reply, error, 500);
      };
    },
    response: {
      failAction = (request, reply, source, error) => {
        return responseDecorator.sendError(request.context, reply, error, 500);
      };
    }
  }
}

Without the failAction's, when Joi Validation fails, onPreResponse is never called and the requestId is never added to the response header. I am currently able to do this on the request side by implementing failAction, but on the response side the function is not implemented.

I took a stab at it locally and it worked well.

alexspence@bdaad8f

@alexspence
Copy link
Author

@alexspence alexspence commented Jun 24, 2016

@hueniverse Let me know if you need more information.

@alexspence
Copy link
Author

@alexspence alexspence commented Aug 8, 2016

Not to pester, but any way to get some attention on this issue?

@hueniverse hueniverse added this to the 15.0.0 milestone Aug 23, 2016
@hueniverse hueniverse self-assigned this Aug 23, 2016
@hueniverse hueniverse closed this Aug 23, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants