sentry-ruby: Exceptions not sent to Sentry from development environment (better_errors)

In development environment, only when exception is captured explicitly using Raven.capture, the exception is getting sent to Sentry and shown in the list of exceptions.

But if config.consider_all_requests_local = false in development.rb, the exceptions are sent.

Related issue - https://github.com/getsentry/raven-ruby/issues/202

Gem versions: rails-4.2.7.1 sentry-raven-2.6.2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 18 (4 by maintainers)

Commits related to this issue

Most upvoted comments

I have same problem and for me it’s caused by better_errors gem.

Simply add better_errors gem to your script to reproduce.

Raven inserts its middleware, Raven::Rack, in the first (outermost) position. This is desirable because it catches any exceptions that bubble up, especially those that happen in the middleware stack itself.

BetterErrors inserts its middleware, BetterErrors::Middleware, just after DebugExceptions, if it is defined. In the PR that put it there, it’s mentioned that this is nice since BetterErrors will render a page for exceptions that originate in all middlewares that are after DebugExceptions. It must be after DebugExceptions itself, however, since DebugExceptions would otherwise swallow the exception before BetterErrors could access it.

In order to prevent DebugExceptions from swallowing the exception, Raven instead patches ActionDispatch::DebugExceptions and ActionDispatch::ShowExceptions in that way that makes it so that errors are reported to Sentry first before rendering the error page.

So, one way to add compatibility for BetterErrors to Raven is to patch BetterErrors::Middleware in a similar way that Raven currently does to ActionDispatch::DebugExcecptions and ActionDispatch::ShowExceptions.

In a Rails initializer,

# https://github.com/getsentry/raven-ruby/issues/738#issuecomment-592720365
if defined?(::BetterErrors::Middleware)
  module BetterErrorsRavenCompatibility
    def show_error_page(env, exception = nil)
      begin
        Raven::Rack.capture_exception exception, env if exception
      rescue StandardError # rubocop:disable Lint/HandleExceptions
      end

      super
    end
  end

  Rails.application.config.after_initialize do
    if Raven.configuration.rails_report_rescued_exceptions
      BetterErrors::Middleware.prepend BetterErrorsRavenCompatibility
    end
  end
end

This works, but since it’s basically a monkey-patch to BetterErrors, it relies on private API that might change between versions.

It’s not clear what the best long-term fix is, though. It doesn’t really make sense for BetterErrors to automatically detect Raven and to automatically report the error. Similarly, I could understand if maintainers of Raven didn’t want to add custom code for maintaining BetterErrors support.

The right fix might just be to add documentation for Raven somewhere that includes a patch like this.

@cjlarose I don’t maintain this anymore (someone else at Sentry does) but just wanted to leave a comment: you leave some of the amazingly clear and detailed explanations of bugs on OSS repos that I have ever seen.

Hi, I also encountered the same issue. I don’t have better_errors gem in my project.

What I did was to declare this in my controller

      def index
        1/0
      end

and have the following set in my sentry.rb

Raven.configure do |config|
  config.dsn = *******
  config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s)
  config.environments = %w[development]
  config.excluded_exceptions = []
end

If I invoke Raven.send_capture(), then it works.

OK. For info here is our initializer after updating from sentry-raven to sentry-ruby 4.1.1:

# https://github.com/getsentry/sentry-ruby/issues/738#issuecomment-750323266
if defined?(::BetterErrors::Middleware)
  module BetterErrorsSentryCompatibility
    def show_error_page(env, exception = nil)
      begin
        Sentry::Rack.capture_exception exception, env if exception
      rescue StandardError # rubocop:disable Lint/HandleExceptions
      end

      super
    end
  end

  Rails.application.config.after_initialize do
    if Sentry.configuration.rails.report_rescued_exceptions
      BetterErrors::Middleware.prepend BetterErrorsSentryCompatibility
    end
  end
end

Good news: I came up with another way to address these kinds of issue in https://github.com/getsentry/sentry-ruby/pull/1168. The PR should fix this issue and will be included in version 4.1.2 of sentry-rails.

However, I won’t backport this fix to the old sentry-raven SDK as this is not a critical issue. So I highly recommend you to migrate to the new SDK by following this guideline.