Skip to content

Commit

Permalink
Fix exception event sending failed (#2083)
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 authored Oct 1, 2023
1 parent a024558 commit 9f3567c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- 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 Sidekiq tracing headers not being overwritten in case of schedules and retries [#2118](https://github.com/getsentry/sentry-ruby/pull/2118)
- 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
60 changes: 60 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,66 @@ 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
begin
raise "#{message}\x1F\xE6"
rescue => e
e
end
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

after do
Sentry.exception_locals_tp.disable
end

let(:exception) do
begin
long = "*" * 1022 + "\x1F\xE6" + "*" * 1000
foo = "local variable \x1F\xE6"
raise message
rescue => e
e
end
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][:long]).to eq("*" * 1022 + "\x1F\uFFFD" + "...")
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][:long]).to eq("*" * 1022 + "\x1F\uFFFD" + "...")
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 9f3567c

Please sign in to comment.