Skip to content

Commit 9e94b6a

Browse files
committed
exceptions_app is called with safe format
1 parent 42a71a0 commit 9e94b6a

File tree

2 files changed

+1
-39
lines changed

2 files changed

+1
-39
lines changed

actionpack/lib/action_dispatch/middleware/public_exceptions.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ def initialize(public_path)
2525
def call(env)
2626
request = ActionDispatch::Request.new(env)
2727
status = request.path_info[1..-1].to_i
28-
begin
29-
content_type = request.formats.first
30-
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
31-
content_type = Mime[:text]
32-
end
28+
content_type = request.formats.first
3329
body = { status: status, error: Rack::Utils::HTTP_STATUS_CODES.fetch(status, Rack::Utils::HTTP_STATUS_CODES[500]) }
3430

3531
render(status, content_type, body)

guides/source/configuring.md

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -370,40 +370,6 @@ Sets up the application-wide encoding. Defaults to UTF-8.
370370
Sets the exceptions application invoked by the `ShowException` middleware when an exception happens.
371371
Defaults to `ActionDispatch::PublicExceptions.new(Rails.public_path)`.
372372

373-
Exceptions applications need to handle `ActionDispatch::Http::MimeNegotiation::InvalidType` errors, which are raised when a client sends an invalid `Accept` or `Content-Type` header.
374-
The default `ActionDispatch::PublicExceptions` application does this automatically, setting `Content-Type` to `text/html` and returning a `406 Not Acceptable` status.
375-
Failure to handle this error will result in a `500 Internal Server Error`.
376-
377-
Using the `Rails.application.routes` `RouteSet` as the exceptions application also requires this special handling.
378-
It might look something like this:
379-
380-
```ruby
381-
# config/application.rb
382-
config.exceptions_app = CustomExceptionsAppWrapper.new(exceptions_app: routes)
383-
384-
# lib/custom_exceptions_app_wrapper.rb
385-
class CustomExceptionsAppWrapper
386-
def initialize(exceptions_app:)
387-
@exceptions_app = exceptions_app
388-
end
389-
390-
def call(env)
391-
request = ActionDispatch::Request.new(env)
392-
393-
fallback_to_html_format_if_invalid_mime_type(request)
394-
395-
@exceptions_app.call(env)
396-
end
397-
398-
private
399-
def fallback_to_html_format_if_invalid_mime_type(request)
400-
request.formats
401-
rescue ActionDispatch::Http::MimeNegotiation::InvalidType
402-
request.set_header "CONTENT_TYPE", "text/html"
403-
end
404-
end
405-
```
406-
407373
#### `config.file_watcher`
408374

409375
Is the class used to detect file updates in the file system when `config.reload_classes_only_on_change` is `true`. Rails ships with `ActiveSupport::FileUpdateChecker`, the default, and `ActiveSupport::EventedFileUpdateChecker`. Custom classes must conform to the `ActiveSupport::FileUpdateChecker` API.

0 commit comments

Comments
 (0)