Skip to content
This repository was archived by the owner on Feb 12, 2026. It is now read-only.

Commit a786def

Browse files
committed
Pass JSON-compatible hashes to async
Many background job processors may throw exceptions when encountering types they cannot serialize - ActiveJob is particularly restrictive. This change passes a *JSON-compatible hash* of only basic types to the `async` callback, so any background job service can serialize it.
1 parent d53387b commit a786def

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

lib/raven/event.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ def get_culprit(frames)
247247
"#{lastframe.filename} in #{lastframe.function} at line #{lastframe.lineno}" if lastframe
248248
end
249249

250+
def to_json_compatible
251+
JSON.parse(JSON.generate(to_hash))
252+
end
253+
250254
# For cross-language compat
251255
class << self
252256
alias captureException from_exception

lib/raven/instance.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ def capture_type(obj, options = {})
120120
yield evt if block_given?
121121
if configuration.async?
122122
begin
123-
configuration.async.call(evt)
123+
# We have to convert to a JSON-like hash, because background job
124+
# processors (esp ActiveJob) may not like weird types in the event hash
125+
configuration.async.call(evt.to_json_compatible)
124126
rescue => ex
125127
Raven.logger.error("async event sending failed: #{ex.message}")
126128
send_event(evt)

spec/raven/event_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,24 @@ def raven_context
331331
end
332332
end
333333

334+
describe '.to_json_compatible' do
335+
subject do
336+
Raven::Event.new(:extra => {
337+
'my_custom_variable' => 'value',
338+
'date' => Time.utc(0),
339+
'anonymous_module' => Class.new
340+
})
341+
end
342+
343+
it "should coerce non-JSON-compatible types" do
344+
json = subject.to_json_compatible
345+
346+
expect(json["extra"]['my_custom_variable']).to eq('value')
347+
expect(json["extra"]['date']).to be_a(String)
348+
expect(json["extra"]['anonymous_module']).not_to be_a(Class)
349+
end
350+
end
351+
334352
describe '.capture_message' do
335353
let(:message) { 'This is a message' }
336354
let(:hash) { Raven::Event.capture_message(message).to_hash }

spec/raven/instance_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'raven/instance'
33

44
describe Raven::Instance do
5-
let(:event) { double("event", :id => "event_id") }
5+
let(:event) { Raven::Event.new(:id => "event_id") }
66
let(:options) { double("options") }
77
let(:context) { nil }
88

@@ -60,7 +60,7 @@
6060
expect(Raven::Event).to receive(:from_message).with(message, options)
6161
expect(subject).not_to receive(:send_event).with(event)
6262

63-
expect(subject.configuration.async).to receive(:call).with(event)
63+
expect(subject.configuration.async).to receive(:call).with(event.to_json_compatible)
6464
subject.capture_type(message, options)
6565
end
6666

@@ -100,7 +100,7 @@
100100
expect(Raven::Event).to receive(:from_exception).with(exception, options)
101101
expect(subject).not_to receive(:send_event).with(event)
102102

103-
expect(subject.configuration.async).to receive(:call).with(event)
103+
expect(subject.configuration.async).to receive(:call).with(event.to_json_compatible)
104104
subject.capture_type(exception, options)
105105
end
106106

@@ -121,7 +121,7 @@
121121
it 'sends the result of Event.capture_exception via fallback' do
122122
expect(Raven::Event).to receive(:from_exception).with(exception, options)
123123

124-
expect(subject).to receive(:send_event).with(event)
124+
expect(subject.configuration.async).to receive(:call).with(event.to_json_compatible)
125125
subject.capture_type(exception, options)
126126
end
127127
end

spec/raven/raven_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'spec_helper'
22

33
describe Raven do
4-
let(:event) { double("event", :id => "event_id") }
4+
let(:event) { Raven::Event.new(:id => "event_id") }
55
let(:options) { double("options") }
66

77
before do
@@ -39,7 +39,7 @@
3939
expect(Raven::Event).to receive(:from_message).with(message, options)
4040
expect(Raven).not_to receive(:send_event).with(event)
4141

42-
expect(Raven.configuration.async).to receive(:call).with(event)
42+
expect(Raven.configuration.async).to receive(:call).with(event.to_json_compatible)
4343
Raven.capture_message(message, options)
4444
end
4545

@@ -78,7 +78,7 @@
7878
expect(Raven::Event).to receive(:from_exception).with(exception, options)
7979
expect(Raven).not_to receive(:send_event).with(event)
8080

81-
expect(Raven.configuration.async).to receive(:call).with(event)
81+
expect(Raven.configuration.async).to receive(:call).with(event.to_json_compatible)
8282
Raven.capture_exception(exception, options)
8383
end
8484

0 commit comments

Comments
 (0)