Skip to content

TypeError when payload class is already a constant #2564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- Fix argument serialization for ranges that consist of ActiveSupport::TimeWithZone ([#2548](https://github.com/getsentry/sentry-ruby/pull/2548))
- Prevent starting Vernier in nested transactions ([#2528](https://github.com/getsentry/sentry-ruby/pull/2528))
- Fix TypeError when Resque.inline == true ([#2564] https://github.com/getsentry/sentry-ruby/pull/2564)

### Miscellaneous

Expand Down
22 changes: 12 additions & 10 deletions sentry-resque/lib/sentry/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,7 @@ def record(queue, worker, payload, &block)

finish_transaction(transaction, 200)
rescue Exception => exception
klass = if payload["class"].respond_to?(:constantize)
payload["class"].constantize
else
Object.const_get(payload["class"])
end

raise if Sentry.configuration.resque.report_after_job_retries &&
defined?(::Resque::Plugins::Retry) == "constant" &&
klass.is_a?(::Resque::Plugins::Retry) &&
!klass.retry_limit_reached?
raise_if_retries_remain(payload["class"], exception)

::Sentry::Resque.capture_exception(exception, hint: { background: false })
finish_transaction(transaction, 500)
Expand Down Expand Up @@ -93,6 +84,17 @@ def finish_transaction(transaction, status)
transaction.set_http_status(status)
transaction.finish
end

private

def raise_if_retries_remain(klass, exception)
return unless Sentry.configuration.resque.report_after_job_retries &&
defined?(::Resque::Plugins::Retry) == "constant"
klass = klass.constantize if klass.respond_to?(:constantize) # Needed for Ruby < 2.7 && Rails < 7.0
klass = Object.const_get(klass) unless klass.is_a?(Class)

raise exception if klass.is_a?(::Resque::Plugins::Retry) && !klass.retry_limit_reached?
end
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions sentry-resque/spec/sentry/resque_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ def self.perform(msg)
expect(event[:tags]).to eq({ "resque.queue" => "default" })
end
end

context "with Resque.inline = true" do
around do |example|
Resque.inline = true
example.run
Resque.inline = false
end

it 'reports the class properly' do
expect do
Resque::Job.create(:default, FailedRetriableJob)
process_job(worker)
end.not_to raise_error(TypeError)

event = transport.events.last.to_hash
expect(event.dig(:exception, :values, 0, :type)).to eq("ZeroDivisionError")
end
end
end

rails_gems = begin
Expand Down
Loading