Understanding Rails

Understanding Rails · Season 4: Controllers, routing and auth

Errors, rescue_from and error pages

Episode 33 · deep dive · about 3 minutes · Rails 8.1 · by

How was this episode?

The series' author reads every note, and no one else sees it. When something is wrong or unclear, it gets fixed and the episode re-cut. How this series stays right.

Was it clear?

Not endorsed yet. Are you a Rails developer? What's an endorsement?

Endorse this version To endorse: create an account, confirm your email, then link GitHub.

What changed

  • Version 2, November 20, 2026: Errors Rails maps to a status, like a 404, go unreported only in production. In development they're reported.

Turning failures into the right status and a useful page.

Free to watch. If it helped, support the series.

Transcript

0:00 Errors, rescue_from and error pages

Episode thirty three. Errors, rescue from, and error pages. Something will go wrong. A note that isn't there, a bad request, a disk that fills up. Today: how Rails turns each failure into the right status, what a person sees, and how you hear about it.

0:21 Exceptions and statuses

Rails keeps a map from exceptions to statuses. Record not found is not found, four oh four. Parameter missing, from episode twenty eight, is bad request. Too many requests, from episode thirty two, is four twenty nine. A stale object, from episode twenty four, is conflict. Anything not on the map is a five hundred.

0:43 In development

The same missing note looks different in each environment. In development, you get four oh four, with the debug page: record not found, in notes controller show, with the stack and the request. That page is for you.

0:58 In production

In production, the debug page would show your code to strangers. So production shows the static page from the public folder instead: the page you were looking for doesn't exist. The setting that decides is consider all requests local: true in development, false in production.

1:16 The error pages

A new Rails app writes those pages for you: four hundred, four oh four, four twenty two, five hundred, and one for unsupported browsers. They're plain HTML, with their own styles, so they still work when the app itself can't render anything. Edit them to match your site.

1:34 A 500

An error that's not on the map becomes a five hundred. Here, an action raises: the disk is full. Production answers with the five hundred page: we're sorry, but something went wrong.

1:47 rescue_from

Rescue from lets one controller handle an error its own way. Rescue record not found, with a method that renders a friendlier page, and keep the status: render not found, status not found. Now a missing note says that note isn't here, with a link back, and the status is still four oh four.

2:07 Keep the status honest

Keep the status honest. Redirecting to the list with an alert looks friendly, but it answers three oh two, so browsers, search engines, and your own tests all think it worked.

2:20 The error reporter

How do you hear about errors? Rails has an error reporter, Rails dot error, and error trackers like AppSignal subscribe to it. Every error that nothing rescued reaches it on its own: the disk is full, handled false. A four oh four isn't, by design: in production, errors on the map aren't reported, because they aren't server errors. But an error you rescue yourself is handled, so it's never reported, unless you report it.

2:46 report and handle

So report what you rescue. Rails dot error dot report, with handled true, and context, like the note's id. And for small things you can recover from, Rails dot error dot handle runs a block, returns a fallback if it raises, and reports it as a warning.

3:04 The rules

So: let the map choose the status. Show static error pages in production, and edit them. Rescue only what you can handle well, keep the status honest, and report what you rescue.

3:16 Next

That ends season four. Next, season five: views and the front end.

Check your understanding

  1. A request raises ActionController::ParameterMissing and nothing rescues it. What status does the response have?

    • 500
    • 400
    • 422
    Show answer

    400. Rails maps ParameterMissing to :bad_request in its rescue_responses. Errors not on that map become 500. Source: ActionDispatch::ExceptionWrapper (Rails source, v8.1.4.1).

  2. Why does production show public/404.html instead of the debug page?

    • Because config.consider_all_requests_local is false there
    • Because production can't render ERB on errors
    • Because the debug page needs a database
    Show answer

    Because config.consider_all_requests_local is false there. With consider_all_requests_local false, Rails shows the static error pages instead of details that would expose your code. Source: Configuring Rails: consider_all_requests_local (Rails Guides 8.1).

  3. In production, a RecordNotFound becomes a 404. Does it reach Rails.error and your error tracker?

    • Yes, every exception is reported
    • Only if it's logged
    • No: errors on the rescue_responses map aren't reported, since they aren't server errors
    Show answer

    No: errors on the rescue_responses map aren't reported, since they aren't server errors. In production, the error reporter reports unhandled errors, but leaves out those in rescue_responses, because they generally aren't bugs. In development, code reloading reports them too. Source: Error Reporting in Rails Applications (Rails Guides 8.1).

  4. What does Rails.error.handle do when its block raises?

    • Re-raises the error after reporting it
    • Reports the error, swallows it, and returns the fallback if one is given
    • Ignores the error without reporting it
    Show answer

    Reports the error, swallows it, and returns the fallback if one is given. handle reports any error raised in the block, then swallows it, so the code after the block carries on. Source: Error Reporting in Rails Applications (Rails Guides 8.1).

Sources

Every example in this episode was run on Rails 8.1.4.1 and Ruby 4.0.2.

Comments

Sign in to comment or sign up.