rails/rails — pull request #7424 by szimek (2012-08-22 20:13:57+00:00)
Title: Allow to set custom error handler for ActionDispatch::ParamsParser.

This allows to set a custom response (e.g. 400) when `ParamsParser` raises an exception when parsing request params. One can do it by swapping the default `ActionDispatch::ParamsParser`:

```ruby
config.middleware.swap ActionDispatch::ParamsParser,
ActionDispatch::ParamsParser, {}, lambda { |e| [400, {}, ["Bad request"]]}
```

This is still a bit awkward, so it could be further simplified to e.g.
```ruby
config.action_dispatch.params_parser_error_handler = lambda { ... }
```
but it's not implemented in this patch.

Without this patch it's of course still possible to return custom response, however slightly more difficult. Here's one of possible solutions:

```ruby
class MyParamsParser < ActionDispatch::ParamsParser
def call
super
rescue
[400, {}, ["Bad Request"]]
end
end

config.middleware.swap ActionDispatch::ParamsParser, MyParamsParser
```

The other question is why Rails doesn't return 400 by default instead of 500 when parsing request params fails :)