Ask A Question

Notifications

You’re not receiving notifications from this thread.

Better Errors ignore 404 errors

Brian Schwartz asked in Rails

I'm using better_errors gem to track errors better. Any idea how to make it ignore 404 or ActiveRecord::RecordNotFound exceptions? I want to test to make sure my 404 page is working correctly and I'm seeing better errors screens instead.

Thanks!

Reply

I think it's always going to thrown an exception in development because that's internal to Rails. You can navigate to /404 to see your 404 page and Rails will catch any ActionController::RoutingError or ActiveRecord::RecordNotFound and render the 404 when in production mode.

I guess you could either run in production mode locally (the real true test) or you could rescue_from those exceptions. Something like so:

rescue_from ActiveRecord::RecordNotFound, :with => :rescue404
 rescue_from ActionController::RoutingError, :with => :rescue404

  def test_404
    render file: 'public/404.html', status: :not_found, layout: false    
  end
Reply

I guess that works (testing 404 manually). I'm intentionally throwing a ActiveRecord::RecordNotFound when a page in my app is visited but not published yet. So I wanted to make sure it was throwing it. I'm getting the Better_Errors error page with that exception, so I guess it's working.

Reply

Yeah, not much better way to test it that I know of unfortunately. As long as you're throwing that exception, you shouldn't have any problems.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 83,603+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.