grape
grape copied to clipboard
Grape::Exceptions::ValidationErrors context
How I can get context of error?
For example: I want know entry point (request_path, route, etc) or resource where the validation exception was handled?
Right now looks like I can do that using custom exception_handler and doing stuff like that:
rescue_from Grape::Exceptions::ValidationErrors do |e|
App::Exceptions::ValidationHandler.new(e, self.env["api.endpoint"].env["REQUEST_PATH"], self.env["api.endpoint"].env["REQUEST_METHOD"]).produce
end
Is that only way that exist right now? What about adding context to errors? (I can send a PR for that)
Adding context to the errors would make total sense, looking forward to a PR.
@g3d +1 for this feature. Had a chance for a PR?
@g3d +1 from me too
@g3d and +1 from me too
Which one of you is attempting a pull request @xevix @jmondo @RooTooZ ?
Hi @dblock, I can explain to you with an example: I have
params do
# some code here
optional :images, type: Array do
optional :image, type: File
optional :id, type: Integer
optional :_destroy, type: Boolean, :desc => 'Destroy that?'
at_least_one_of :id, :image
all_or_none_of :_destroy, :id
end
optional :ingredients, type: Array do
optional :id, type: Integer
# some code here
optional :ingredient_id, type: Integer
optional :_destroy, type: Boolean, :desc => 'Destroy that?'
at_least_one_of :id, :ingredient_id
all_or_none_of :_destroy, :id
end
# some code here
end
rescue_from Grape::Exceptions::ValidationErrors do |e|
ap e.as_json # <-- point to debug
# some code here
error!(serializable_resource.as_json, 400)
end
debug log
[
[0] { # <-- this is images validation
:params => [
[0] "_destroy",
[1] "id"
],
:messages => [
[0] "provide all or none of parameters",
[1] "provide all or none of parameters"
]
},
[1] { # <-- this is ingredients validation
:params => [
[0] "_destroy",
[1] "id"
],
:messages => [
[0] "provide all or none of parameters",
[1] "provide all or none of parameters"
]
}
]
As you can see, I don’t have information about which rule the exception worked for. I can only guess that the top one is for pictures, and the other is for ingredients. That the problem.
Thanks!
I understand the problem. I am looking at you (or other people with this problem) to write a spec and implement a fix.