grape icon indicating copy to clipboard operation
grape copied to clipboard

Grape::Exceptions::ValidationErrors context

Open g3d opened this issue 11 years ago • 7 comments

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)

g3d avatar Feb 12 '14 17:02 g3d

Adding context to the errors would make total sense, looking forward to a PR.

dblock avatar Feb 13 '14 12:02 dblock

@g3d +1 for this feature. Had a chance for a PR?

xevix avatar Mar 23 '14 12:03 xevix

@g3d +1 from me too

jmondo avatar Sep 26 '14 03:09 jmondo

@g3d and +1 from me too

RooTooZ avatar Apr 18 '19 05:04 RooTooZ

Which one of you is attempting a pull request @xevix @jmondo @RooTooZ ?

dblock avatar Apr 18 '19 13:04 dblock

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!

RooTooZ avatar Apr 19 '19 05:04 RooTooZ

I understand the problem. I am looking at you (or other people with this problem) to write a spec and implement a fix.

dblock avatar Apr 19 '19 11:04 dblock