Sentry-ruby: Sentry raises `NoMethodError: undefined method 'event_id' for false:FalseClass` on calling Sentry.capture_exception in Rails integration tests

Created on 22 Dec 2020  路  3Comments  路  Source: getsentry/sentry-ruby

Describe the bug

  1. A controller is called in a Rails MiniTest integration test.
  2. The controller catches an exception and calls Sentry.capture_exception.
  3. Sentry throws a NoMethodError which causes the controller to fail with a 500 error:
Minitest::UnexpectedError:         NoMethodError: undefined method `event_id' for false:FalseClass
            /usr/local/bundle/gems/sentry-ruby-4.1.0/lib/sentry/hub.rb:113:in `capture_event'
            /usr/local/bundle/gems/sentry-ruby-4.1.0/lib/sentry/hub.rb:85:in `capture_exception'
            /usr/local/bundle/gems/sentry-ruby-4.1.0/lib/sentry-ruby.rb:119:in `capture_exception'
            /usr/local/bundle/gems/sentry-ruby-4.1.0/lib/sentry/rack/capture_exceptions.rb:28:in `rescue in block in call'
            /usr/local/bundle/gems/sentry-ruby-4.1.0/lib/sentry/rack/capture_exceptions.rb:22:in `block in call'
            /usr/local/bundle/gems/sentry-ruby-4.1.0/lib/sentry/hub.rb:50:in `with_scope'
            /usr/local/bundle/gems/sentry-ruby-4.1.0/lib/sentry-ruby.rb:103:in `with_scope'
            /usr/local/bundle/gems/sentry-ruby-4.1.0/lib/sentry/rack/capture_exceptions.rb:14:in `call'
            /usr/local/bundle/gems/railties-5.1.7/lib/rails/engine.rb:522:in `call'
            /usr/local/bundle/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
            /usr/local/bundle/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
            /usr/local/bundle/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
            /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/testing/integration.rb:261:in `process'
            /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/testing/integration.rb:28:in `patch'
            /usr/local/bundle/gems/actionpack-5.1.7/lib/action_dispatch/testing/integration.rb:348:in `block (2 levels) in <module:Runner>'
            test/controllers/my_controller_test.rb:63:in `block in <class:MyControllerTest>'

Looking at line 113 in sentry/hub.rb, it expects current_client.capture_event to either return nil or return an object that responds to event.event_id. However, as the error indicates, it actually got false:FalseClass instead.

Looking at line 85 in sentry/hub.rb and Client.event_from_exception in sentry/client.rb, it looks like event should always be either nil or an instance of Event.

To Reproduce

  1. Create a controller that calls Sentry.capture_exception in a method.
  2. Set up a route to the controller method created in step 1.
  3. Create a Rails MiniTest integration test that sends a request to the route defined in step 2.

Controller:

class MyController < ApplicationController
    def data
      Sentry.capture_exception(RuntimeError.new('Hello, World'))
      render json: { message: 'Hello, World!' }
    end
end

Router:

Rails.application.routes.draw do
  get 'my/data', to: 'my#data'
end

Integration test:

require 'test_helper'

class MyControllerTest < ActionDispatch::IntegrationTest
  test 'send request to our controller' do
    get "my/data"
  end
end

Expected behavior
Sentry should capture the exception without raising any errors.

Actual behavior
A NoMethodError is raised.

Environment

  • Ruby Version: 2.7.2
  • SDK Version: 4.1.0
  • Rails 5.1.7

Sentry Config

Sentry.init do |config|
  config.dsn = Rails.application.config.sentry_dsn if Rails.env.production?
end
bug

Most helpful comment

@burgaard thanks for the detailed reporting!
this bug should have been fixed in 4.1.1, can you upgrade both sentry-ruby and sentry-rails to 4.1.1 and try it again?

All 3 comments

@burgaard thanks for the detailed reporting!
this bug should have been fixed in 4.1.1, can you upgrade both sentry-ruby and sentry-rails to 4.1.1 and try it again?

closing now as it has been fixed. feel free to reopen this if you still see the issue after upgrading the SDK.

Thanks @st0012

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benebrice picture benebrice  路  5Comments

SirRawlins picture SirRawlins  路  7Comments

lewisifer picture lewisifer  路  4Comments

jonhue picture jonhue  路  5Comments

robertlyall picture robertlyall  路  4Comments