views:

194

answers:

1

What I want to do: redirect the user to a special error page if the database is down.

I'm using an Oracle database with the OCI adapter.

It appears that, if the database is unavailable (say, down for backup), that the OCI adapter throws an error before I ever hit a controller (based on the stack trace, it's while it's setting up the connection pool). So I can't use rescue_from or rescue_action, even in ApplicationController -- the rescue line is never reached.

Is there any way to "wrap" the initialization or otherwise rescue a particular error at a higher level (or earlier point) than ApplicationController?

+1  A: 

Try overriding ActionController::Failsafe, or sticking another middleware before it on the stack, you should be able to catch almost all exceptions from there, and 302 at will.

Example:

class DbFailsafe
  def new(app)
    @app = app
  end

  def call(env)
    @app.call(env)
  rescue DataBaseError => e
    [302, {"Content-Type" => "text/plain", "Location" => "http://otherserver.com/sorrt-the-site-is-down.html"}, ["You are being redirected to http://otherserver.com/sorrt-the-site-is-down.html"]]
  end
end

and in your environment.rb

config.middleware.insert_after ::ActionController::Failsafe, DbFailsafe

I'd recommend (because a we should be returing a 5xx not a 3xx) rendering a simple page with a js redirect. This is simple to do, just edit public/500.html.

cwninja