Suppress exception in browser
I have to debug some error that is db related and have been continuously monitoring the log when there is an error c开发者_如何转开发aused 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!!!
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).
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
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.
精彩评论