elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 84 forks source link

Dealing with multiple missing fields when rescuing InvalidFormat exception #124

Closed b-hass closed 4 years ago

b-hass commented 5 years ago

Currently if I have a params block for some route like this:

params do
    requires "param_a", type: String
    requires "param_b", type: String
end

and a rescue block like this:

rescue_from Maru.Exceptions.InvalidFormat, as: e do
    ...
end

When I send a request with none of the parameters I get the following exception:

%Maru.Exceptions.InvalidFormat{
  param: "param_a",
  plug_status: 400,
  reason: :required,
  value: nil
}

This is good, but it would be much better if the exception contained a list with all the parameters that were not matched. Is this behaviour possible currently?

EDIT:

I was reading the code base and came up with the following idea: perhaps in the parse_params function in lib/maru/builder/plug_router/runtime.ex we could delay the exception thrown by the blank_func and first reduce through all the errors and throw an exception at the end?