Skip to content

Commit

Permalink
Fix exception event sending failed
Browse files Browse the repository at this point in the history
This fixes `Event sending failed: source sequence is illegal/malformed utf-8`.
  • Loading branch information
yujideveloper committed Sep 27, 2023
1 parent efcc58a commit f73015d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- Always send envelope trace header from dynamic sampling context [#2113](https://github.com/getsentry/sentry-ruby/pull/2113)
- Improve `TestHelper`'s setup/teardown helpers ([#2116](https://github.com/getsentry/sentry-ruby/pull/2116))
- Fixes [#2103](https://github.com/getsentry/sentry-ruby/issues/2103)
- Fix exception event sending failed due to source sequence is illegal/malformed utf-8 [#2083](https://github.com/getsentry/sentry-ruby/pull/2083)
- Fixes [#2082](https://github.com/getsentry/sentry-ruby/issues/2082)

## 5.11.0

Expand Down
4 changes: 2 additions & 2 deletions sentry-ruby/lib/sentry/interfaces/single_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(exception:, stacktrace: nil)
exception.message || ""
end

@value = exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES)
@value = Utils::EncodingHelper.encode_to_utf_8(exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES))

@module = exception.class.to_s.split('::')[0...-1].join('::')
@thread_id = Thread.current.object_id
Expand Down Expand Up @@ -51,7 +51,7 @@ def self.build_with_stacktrace(exception:, stacktrace_builder:)
v = v.byteslice(0..MAX_LOCAL_BYTES - 1) + OMISSION_MARK
end

v
Utils::EncodingHelper.encode_to_utf_8(v)
rescue StandardError
PROBLEMATIC_LOCAL_VALUE_REPLACEMENT
end
Expand Down
49 changes: 49 additions & 0 deletions sentry-ruby/spec/sentry/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,55 @@ module ExcTag; end
end
end
end

describe "bad encoding character handling" do
context "if exception message contains illegal/malformed encoding characters" do
let(:exception) do
raise "#{message}\x1F\xE6"
rescue => e
e
end

it "scrub bad encoding error message" do
expect { event.to_json_compatible }.not_to raise_error
version = Gem::Version.new(RUBY_VERSION)
if version >= Gem::Version.new("3.2")
expect(hash[:exception][:values][0][:value]).to eq("#{message}\x1F\uFFFD (RuntimeError)")
else
expect(hash[:exception][:values][0][:value]).to eq("#{message}\x1F\uFFFD")
end
end
end

context "if local variable contains illegal/malformed encoding characters" do
before do
perform_basic_setup do |config|
config.include_local_variables = true
end
end

let(:exception) do
foo = "local variable \x1F\xE6"
raise message
rescue => e
e
end

it "scrub bad encoding characters" do
expect { event.to_json_compatible }.not_to raise_error
version = Gem::Version.new(RUBY_VERSION)
if version >= Gem::Version.new("3.2")
expect(hash[:exception][:values][0][:value]).to eq("#{message} (RuntimeError)")
frames = hash[:exception][:values][0][:stacktrace][:frames]
expect(frames[-1][:vars][:foo]).to eq "local variable \x1F\uFFFD"
else
expect(hash[:exception][:values][0][:value]).to eq(message)
frames = hash[:exception][:values][0][:stacktrace][:frames]
expect(frames[-1][:vars][:foo]).to eq "local variable \x1F\uFFFD"
end
end
end
end
end

describe "#generate_sentry_trace" do
Expand Down

0 comments on commit f73015d

Please sign in to comment.