Forum: Ruby on Rails Handling/Catching Exceptions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9618e8fca8d72bf01814276c53c2568c?d=identicon&s=25 Robert MannI (Guest)
on 2006-03-16 23:26
(Received via mailing list)
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
D90ef6808433e63203e15a5c2dadb0bb?d=identicon&s=25 Ben Reubenstein (Guest)
on 2006-03-16 23:41
(Received via mailing list)
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 <robmnl@gmail.com> wrote:
>
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Ben Reubenstein
http://www.benr75.com
9618e8fca8d72bf01814276c53c2568c?d=identicon&s=25 Robert MannI (Guest)
on 2006-03-17 14:38
(Received via mailing list)
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
F15fdc7cb2e911b3808837f2be244add?d=identicon&s=25 Adam Denenberg (Guest)
on 2006-03-17 14:41
(Received via mailing list)
i would be interested in this as well.  let me know if you find
anything.

adam
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2006-03-18 10:34
(Received via mailing list)
On 17 Mar 2006, at 14:39, Adam Denenberg 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
This topic is locked and can not be replied to.