getsentry / sentry-ruby

Sentry SDK for Ruby
https://sentry.io/for/ruby
MIT License
927 stars 493 forks source link

Sentry reports exception already caught by assert_raises #2277

Closed amo13 closed 5 months ago

amo13 commented 5 months ago

I do not understand why my test cases following this pattern send exceptions to sentry. The assert_raises statement rescues the exception and does not reraise it. How can it still slip through and be reported to the sentry server? And what do I need to change to a) prevent these job tests to send report exception correctly caught by the assert_raises helper, and b) still report uncaught exceptions in the test environment?

require "test_helper"
require "minitest/mock"

class My::JobTest < ActiveJob::TestCase
  test "raise My::Error" do
    assert_raises My::Error do
      Ext::Api.stub :getsize, {size_gb: 1} do
        My::Job.perform_now
      end
    end
  end
end
class My::Job < ApplicationJob
  queue_as :myqueue
  queue_with_priority 5

  def perform(args)
    raise My::Error
  end
end

I can't imagine that this happens because I have configured GoodJob which is not supported by this gem because the tests use the test adapter anyway. Any guidance with this would be greatly appreciated.

sl0thentr0py commented 5 months ago

this is because we patch active job and thus the sentry stuff runs before your assert_raises https://github.com/getsentry/sentry-ruby/blob/100caa0dc679c4e53564b32a56eee335f61f615f/sentry-rails/lib/sentry/rails/active_job.rb#L4-L12

You can manually set config.enabled_environments = %w[development production], does so that sentry does not send in test, does that work for you or do you need to disable just this one usage?

edit: nvm sorry didn't read that you still want to send uncaught exceptions in tests.

Then your next best way is to use config.before_send and filter out those particular exceptions. https://docs.sentry.io/platforms/ruby/configuration/filtering/#using-

amo13 commented 5 months ago

Thank you a lot for clarifying this!