I've recently set up Rollbar for my rails app. It's reporting the errors but not always with context. In order to get context you need to catch the exception and pass in the error
begin
# code...
rescue => e
Rollbar.error(e)
Is there a rails way to generically catch exceptions with context?
Maybe you wrap application controller with something? In Django you can subclass a view...
Assuming all your controllers inherit from ApplicationController, you can use rescue_from
in your ApplicationController to rescue any errors in any controller.
ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound do |exception|
message = "Couldn't find a record."
redirect_to no_record_url, info: message
end
end
You can have multiple rescue_from
clauses for different error classes, but note that they're invoked in reverse order, so a generic rescue_from
should be listed before others...
ApplicationController < ActionController::Base
rescue_from do |exception|
message = "some unspecified error"
redirect_to rescue_message_url, info: message
end
rescue_from ActiveRecord::RecordNotFound do |exception|
message = "Couldn't find a record."
redirect_to rescue_message_url, info: message
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With