rails/ruby - how to rescue ActionView::TemplateError
-
19-09-2019 - |
Question
i am testing an application built with rails and from time to time get bugs
when it happens, ActionView::TemplateError occurs and gets logged in log/production.log
how do i catch it and f.ex. send myself by email whenever it happens somewhere in the app?
what is the best way to do that?
f.ex. in ApplicationController with some sort of before_filter?
the code would be, i imagine
before_filter :app_error_catch
def app_error_catch
begin
rescue ActionView::TemplateError
Mailer.deliver_apperror(errormessage)
end
end
is that close to truth? and how do i get the "errormessage"?
thank you
Solution
Have you looked at [exception_notifier][1]
or hoptoad? exception_notifier is a plugin that will send an email on an exception, complete with backtrace.
Hoptoad is a website that collects information about your application's exceptions.
OTHER TIPS
You don't have to write this yourself - there are plugins built to do this. As suggested by flyfishr64, exception_notifier is widely used.
Go through the following (short) screencast and see which solution you like the best.
Along the lines of flyfishr64's answer, there's also the lovely Exceptional
There are several tools/services you can use.
Third-party services:
Plugins:
- Exception Logger (almost dead)
Custom
You can easily integrate a custom feature using rescue_from and rescue_action_in_public methods.