In Rails 8.1, how do config.action_dispatch.show_exceptions and config.consider_all_requests_local together decide what a request that raises receives?
answer
- DebugExceptions inside ShowExceptions
- :all, :rescuable, :none
- test.rb generates :rescuable
- booleans removed in 7.2
- show_detailed_exceptions? override
basics
~10 sshow_exceptions decides whether Rails renders a response for an exception (:all, :rescuable for mapped classes only, :none); consider_all_requests_local decides whether that response is the detailed debug page or the public error page.
solid answer
~40 sTwo middlewares handle an escaped exception: `DebugExceptions` inside `ShowExceptions`. Both ask whether to show it at all, which `config.action_dispatch.show_exceptions` decides: `:all` (the default) renders for every exception, `:rescuable` only for classes in `rescue_responses`, `:none` raises everything. If showing is allowed and `consider_all_requests_local` is true, `DebugExceptions` renders the debug page; otherwise `ShowExceptions` calls the exceptions app for `public/<status>.html`. So development shows debug pages, production shows public pages, and the generated `test.rb` uses `:rescuable` so a missing record answers 404 while a `NoMethodError` raises into the test. Since Rails 7.2 the old booleans are gone, and a leftover `false` now acts like `:all`. A controller can override `show_detailed_exceptions?` to show debug pages to chosen requests in production.
code
ruby · 6 lines# config/environments/test.rb (as generated by Rails 8.1)
Rails.application.configure do
config.consider_all_requests_local = true
# Render exception templates for rescuable exceptions and raise for other exceptions.
config.action_dispatch.show_exceptions = :rescuable
endgo deeper
Recall that development shows the debug page and production shows public error pages, because consider_all_requests_local is true in one and false in the other.
Explain the three show_exceptions values, the order of DebugExceptions and ShowExceptions, and why the generated test.rb uses :rescuable.
Diagnose upgrade fallout from the removed booleans, and use a narrow show_detailed_exceptions? override instead of turning detailed pages on in production.
Set the policy for who may ever see internals in production and how tests should treat expected versus unexpected exceptions.
## Two middlewares, two switches When an exception escapes the controller, two middlewares from Rails' default stack decide what happens. **`ActionDispatch::ShowExceptions`** sits outside **`ActionDispatch::DebugExceptions`**, so the exception meets `DebugExceptions` first. - **`config.action_dispatch.show_exceptions`** answers *whether Rails turns the exception into a response at all*. Both middlewares ask `ExceptionWrapper#show?`. - **`config.consider_all_requests_local`** answers *whether that response is the detailed debug page*. Rails copies it into the request env as `action_dispatch.show_detailed_exceptions`. ## The three values of show_exceptions | value | effect | |---|---| | `:all` | render a response for every exception (the default from Action Dispatch's railtie) | | `:rescuable` | render only for classes in `rescue_responses`; raise the rest | | `:none` | raise every exception | "Raise" means the exception leaves the Rails app: the web server logs it and returns its own error, and in an integration test it is raised into the test. ## How a request is decided 1. `DebugExceptions` rescues the exception and asks `show?`. If the answer is no, it re-raises. 2. If showing is allowed and detailed exceptions are on, it renders the **debug page** (HTML for a browser, a structured body for an API request) with the mapped status. 3. Otherwise it logs and re-raises; `ShowExceptions` asks `show?` again and, if allowed, calls the **exceptions app** (by default `public/<status>.html`). ## What a new Rails 8.1 app gets | environment | `consider_all_requests_local` | `show_exceptions` | missing record | `NoMethodError` | |---|---|---|---|---| | development | `true` | `:all` (default) | debug page, 404 | debug page, 500 | | test | `true` | `:rescuable` | debug page, 404 | raised into the test | | production | `false` | `:all` (default) | `public/404.html` | `public/500.html` | The test row is the interesting one: an integration test can assert `assert_response :not_found` for a missing record, while a real bug still fails the test loudly instead of hiding behind a 500 page. ## Version history that bites on upgrade - Up to Rails 7.0 the setting was a boolean, and generated `test.rb` files said `show_exceptions = false`. - **Rails 7.1** introduced `:all`, `:rescuable` and `:none`, deprecated the booleans, and started generating `:rescuable` in `test.rb`. - **Rails 7.2** removed the boolean support. In 8.1 the code checks only for `:none` and `:rescuable`; **any other value behaves like `:all`**. A `false` left over in an old `test.rb` now renders error responses instead of raising, and tests that expected an exception see a 500 response instead. ## Detailed pages for some requests in production `consider_all_requests_local` is all-or-nothing. For finer control, leave it `false` and override **`show_detailed_exceptions?`** in a controller; the Rails source suggests `request.local?`. Rails consults it only when the flag is false, and only for exceptions that pass through that controller's `process_action`, so routing errors are unaffected. Showing backtraces to anyone outside your team leaks source paths and data, so the override should be narrow. ## Reading a surprising result - A debug page in production means `consider_all_requests_local` is true there, or a `show_detailed_exceptions?` override is too broad. - A raw server error instead of any Rails page means `show_exceptions` is `:none`, or `:rescuable` and the class is unmapped. - A correct status with an empty body means the exceptions app has no page or route for that status. ## Side effects of the local flag - It enables **`ActionDispatch::ActionableExceptions`**, the middleware behind buttons such as running pending migrations from the error page. - Rails' guide notes it also exposes runtime details at `/rails/info/properties`. - Neither belongs in production, which is why the generated `production.rb` sets it to `false`.
- After upgrading to Rails 8.1, integration tests that expected an exception now get a 500 response. What happened?The old `test.rb` still says `config.action_dispatch.show_exceptions = false`. Rails 7.2 removed boolean support, and the 8.1 code treats any value other than `:none` or `:rescuable` like `:all`, so the exception is rendered. Change it to `:rescuable`, or `:none` to raise everything.
- Why does an integration test for a missing record see a 404 in a new Rails 8.1 app?`RecordNotFound` is in `rescue_responses`, and the generated `test.rb` sets `show_exceptions = :rescuable`, which renders mapped exceptions. Because `consider_all_requests_local` is also true there, the body is the debug page, but the status is 404, which is what `assert_response :not_found` checks.
- show_exceptions = false still makes Rails 8.1 raise every exception.
- consider_all_requests_local decides whether exceptions are rescued at all.
- :rescuable means Rails rescues every StandardError and raises only Exception.
- show_detailed_exceptions? is consulted even when consider_all_requests_local is true.
- Turning on consider_all_requests_local in production is a safe way to debug users' errors.
Assumes Rails 8.1: symbol values since 7.1, boolean values removed in 7.2, and :rescuable in the generated test.rb.