Fix a bug where malformed query strings lead to 500

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 and it raises an
ActionController::BadRequest from inside ActionDispatch::ShowExceptions,
hence the 500 error.

This commit changes PublicExceptions to catch BadRequest if raised from
request#formats.

fixes #11502
Merge pull request #16886 from yuki24/bugfix-bad-request-from-public-exception-4-1-stable

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