[4-1-stable] Fix a bug where malformed query strings lead to 500

This is similar to #16871 but with a green test.

This is a bugfix for #11502.

If a request contains an malformed query string, Rails responds with an internal server error insted of a bad request. This is because [`ActionDispatch::PublicExceptions` calls `request.formats`](https://github.com/rails/rails/blob/7133b0090e7509025bf71f22fb9f41404031dbbf/actionpack/lib/action_dispatch/middleware/public_exceptions.rb#L12) and [it raises an `ActionController::BadRequest`](https://github.com/rails/rails/blob/7133b0090e7509025bf71f22fb9f41404031dbbf/actionpack/lib/action_dispatch/http/request.rb#L276) from inside `ActionDispatch::ShowExceptions`, hence the 500 error.

This commit changes `PublicExceptions` to catch `BadRequest` if raised from `request.formats`.

On master, #13999 fixed this bug so we don't have to worry about it on master.