Sentry-ruby: Exception App, self.routes, un handled exceptions not reported.

Created on 23 Mar 2018  路  2Comments  路  Source: getsentry/sentry-ruby

In our application we set

config.exceptions_app = self.routes in our application.rb in our rails app.

we also set

Raven.configure do |config|
  config.rails_report_rescued_exceptions = false
end

so that we would not get notified by exceptions handled by our exceptions_app.

However this poses a problem because any errors that our exceptions app did not handle were still being swallowed up and not reported.

Any suggestions on how to handle this with sentry? We used to use Honeybadger and they would always notify with the presence of action_dispatch.exception (HB source) and it was up to use to remove the action_dispatch.exception before the middleware caught it.

So we would do something like this:

class ExceptionsBaseController < ActionController::Base
  after_filter :clear_exceptions

  def clear_exceptions
    exception_keys = ['action_dispatch.exception', 'rack.exception', 'sinatra.error']
    env.delete_if{|k,_| exception_keys.include?(k) }
  end
end

which our exceptions app controller would inherit from. Therefore any exceptions that were routed to our controller would clear the exception and HB would not notify. Anything not routed to our controller (aka 500s) would still be notified.

This worked quite nicely. However we can't find a way to do that with Raven Sentry because if we set rails_report_rescued_exceptions back to true it will report the exception before it reaches our exceptions app. Any thoughts?

In the meantime I think I will add a middleware that can report if action_dispatch.exception is present that runs before the Sentry Rack middleware https://github.com/getsentry/raven-ruby/blob/9649fa5a50e5dcac9df108755990740af2c34822/lib/raven/integrations/rack.rb#L59-L60

question

All 2 comments

Why not just use Raven.capture_exception in your exception app?

There's a long history of us battling problems with action_dispatch.exception. We basically decided in #370 that if you're using a custom exception app, it's "your problem" to report exceptions if you're using a custom Rails exception app.

Why not just use Raven.capture_exception in your exception app?

It's a good question. We did initially do this but we had a few namespaces in our routes file (dashboard/api/admin). some with only rails views. Not every namespace was being routed to the exceptions controller and some errors were being swallowed. we could of course, simply start routing the other namespaces to an exceptions controller to start handling them.

I totally see the point of:

if you're using a custom exception app, it's "your problem" to report exceptions if you're using a custom Rails exception app.

Also our use case is a bit complicated, so wouldn't necessarily expect raven to handle this. I mostly wanted to see if there was another solution out there. Thanks for your help!

For those interested we ended up adding a middleware:

class RavenNotCaughtByExceptionsApp
  def initialize(app)
    @app = app
  end

  def call(env)
    response = @app.call(env)

    error = env['action_dispatch.exception']
    Raven::Rack.capture_exception(error, env) if error

    response
  end
end

And we added

config.middleware.insert_before ActionDispatch::ShowExceptions, RavenNotCaughtByExceptionsApp

@nateberkopec please let me know if you see any immediate pitfalls in our above solution.

Was this page helpful?
0 / 5 - 0 ratings