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

Proposal for a new hook #75

Open
lmasarati opened this issue Aug 1, 2019 · 4 comments
Open

Proposal for a new hook #75

lmasarati opened this issue Aug 1, 2019 · 4 comments

Comments

@lmasarati
Copy link

I would find useful to add a new hook to be called in case of failed validation, to allow for modification of headers, content body before a standard 400 Bad Request response is returned.
It is clear to me how to add such a hook, but not how to get validation errors that now are logged on STDERR.

@lmasarati
Copy link
Author

I propose this pull for the base feature of having a hook specific for failed data validation flow #76

@khrt
Copy link
Owner

khrt commented Aug 20, 2019

Hi @lmasarati, sorry it took that long to reply.

I've checked your PR and I have to reject it, because following this logic Raisin might end up having many-many other different hooks for a lot of other events.

I can think of two possible solutions at the moment, though both of a similar kind:

  1. Do not require a Raisin code change (not tested though should be working) is to add a middleware which would check error code and error message and add headers requested. It requires some kind of heuristics but should be okay in this case.
  2. Extend Raisin's code with adding an additional data structure which will hold information about which hook failed so you can do better detection in 1.

@lmasarati
Copy link
Author

Thanks. Part 2 of the solution should be a good start. Developing a custom Middleware would not be a problem if Raisin publishes the result of validation in some environment variables.

@lmasarati
Copy link
Author

about hooks in general, there's no way to force an immediate response from inside a hook.
For example a custom authorization or authentication could be placed inside the before hook, but in case of failure the request must pass through parameter and route validation before having a chance to be finalized.
What about using the hook return value to force a return $res->finalize ?

my $ret = eval {
    $self->hook('before')->($self) and return $res->finalize;

The hook is then responsible for preparing the status, body, headers etc and returning 1 for forcing an immediate response to caller.
What do you think?

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

No branches or pull requests

2 participants