Possible to render and raise exception in Rails controller? -
i have been fighting hours now. have controller action want render page , raise exception automatically. couldn't find information on makes me think looking wrong thing.
is below possible?
class userscontroller < actioncontroller::base def new # .. stuff begin usermailer::send_confirmation_link(@user) rescue standarderror => e render 'email_error' raise(e) end # .. other stuff end end
in case want inform end-user of error , raise exception on application itself. notice can't change de-facto error page since smaller application in same codebase bigger application.
no, either render or raise exception, not both.
rails provide both static 500.html
page in public
what's rendered default exceptions, can customize message users see exceptions there.
also there's rescue_from
method can use customize response specific exception class, , that's way have central spot (usually in applicationcontroller
) exception responses located.
if doing use case want own custom exception class, subclass of runtimeerror
, wrap exception in re-raise, like:
rescue standarderror => e raise emailconfirmationerror.new e.message end
...and in applicationcontroller
:
rescue_from emailconfirmationerror { |e| render "email_error" }
Comments
Post a Comment