Handling/Catching Exceptions

Hello!

I was wondering, sometimes I raise an exception in my Rails apps, e.g.:

raise SecurityError unless @post.user == logged_in_user

Now: is there any way to catch that SecurityError exception somewhere
and render a custom template? E.g., I want to render
‘app/views/content/security_error.rhtml’

Does anyone know how to do this?

Thanks a bunch,
Rob

Hi Rob ~

You could put it in a begin/rescue block…

begin
SOMETHING THAT MIGHT FAIL HERE
rescue
WHEN IT FAILS DO STUFF HERE
end

~ Ben

On 3/16/06, Robert MannI [email protected] wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails


Ben R.
http://www.benr75.com

i would be interested in this as well. let me know if you find
anything.

adam

Hi Ben!

Thanks, yeah. But I was wondering if there’s a way to globally catch
them. Doesn’t Rails catch things like ActiveRecord::RecordNotFound?
Maybe one can plugin custom error catching there.

If I find anything I will update this thread.

Thanks,
Rob

On 17 Mar 2006, at 14:39, Adam D. wrote:

i would be interested in this as well. let me know if you find
anything.

It’s actually very simple, check out recipe 31 from the Rails Recipes
book: Getting Notified of Unhandled Exceptions. It also elaborates on
how to get notified by mail if an exception occurs.

Basically, you need to override the rescue_action_in_public method
from ActionController.

Best regards

Peter De Berdt