views:

63

answers:

3

Hi guys,

I have to debug some error that is db related and have been continuously monitoring the log when there is an error caused by db. Since the error is already logged, I want to have exceptions like:

ActiveRecord::StatementInvalid

to be suppressed so that user won't see the 'something went wrong page'. The error is limited to a small section of the app and we want to remove this suppression after it is fixed.

Thanks!!!

+3  A: 

You can add a rescue_from line in your ApplicationController to catch that particular exception if it's thrown in any controller. Then you have to decide what should happen in that situation (e.g. redirect to the starting page).

JacobM
sweet that's exactly what i am looking for instead of 'rescue_action_in_public'
penger
A: 

You could wrap the code that's failing in a begin rescue end block.

begin 
  # stuff that gets executed
  ...
  # DangerousStatement
  ...
  # stuff that doesn't get executed if dangerous statement raises an error
  ...
rescue
  # set variables that are needed by other code and would have been set if dangerous statement succeeded
  ...
end

However this is really something you should be debugging in development mode.

EmFi
+1  A: 

Assuming you have a problem in the "create" action of a controller, you could do something like:

def create
  @record.create(params[:record])
rescue ActiveRecord::StatementInvalid
  flash[:notice] = "There was a problem, but we know about it."
  redirect_to root_path
end
bensie