ruby-on-railsrubysentry

How to classify ActionController::RoutingError as an error in Sentry


I am using the new Sentry (not Raven) for Ruby for my Rails 6.1 application:

gem "sentry-ruby"
gem "sentry-rails"

I am able to see the transactions when users triggered ActionController::RoutingError, but I want these to appear as errors in Sentry. I do not see ActionController::RoutingError as an 'ignored' or 'excluded' error:

> Sentry::Configuration::IGNORE_DEFAULT
 => ["Mongoid::Errors::DocumentNotFound", "Rack::QueryParser::InvalidParameterError", "Rack::QueryParser::ParameterTypeError", "Sinatra::NotFound"] 

I tried clearing excluded_exceptions in sentry.rb initializer file, but this had no effect:

Sentry.init do |config|
  ...
  config.excluded_exceptions = []
end

How can I configure Sentry so that these are sent as errors? I was also thinking that I can modify the middleware (ActionDispatch?, ActionPack?) and add Sentry.capture_message where appropriate, but I am not sure how to do that.

I do not want a "catch-all" route that redirects to an ErrorController or ApplicationController:

match '*path', to: "application#handle_route_error", via: :all


Solution

  • The problem is that this error is handled by default in Rails and rescued with "Not Found" response

    Of course you can handle such errors ("Not Found", "Unprocessable Entity", "Internal Server Error", etc.) manually using router and response with specific action of something like ErrorsController. You can send message to Sentry from there

    But since you don't want such decision, you can monkeypatch middleware or even exactly ActionController::RoutingError. Find source of this error and add initializer like this

    # config/initializers/handle_routing_error.rb
    
    module ActionController
      class RoutingError < ActionControllerError
        attr_reader :failures
        def initialize(message, failures = [])
          Sentry.capture_message(message) # or Sentry.capture_error(self)
    
          super(message)
          @failures = failures
        end
      end
    end
    

    Usually monkeypatch is some hack and not nice solution, but it will work