From bebedc4d9227a31f77ae89dec354051846eadb92 Mon Sep 17 00:00:00 2001 From: Akshay Birajdar Date: Sat, 25 Nov 2023 23:36:40 +0530 Subject: [PATCH] Allow non-string error message to be reported to sentry (#2137) --- CHANGELOG.md | 1 + .../lib/sentry/interfaces/single_exception.rb | 1 + sentry-ruby/spec/sentry/client_spec.rb | 12 ++++++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2fe37cd1..f071b43cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - Fixed a deprecation in `sidekiq-ruby` error handler [#2160](https://github.com/getsentry/sentry-ruby/pull/2160) - Avoid invoking ActiveSupport::BroadcastLogger if not defined [#2169](https://github.com/getsentry/sentry-ruby/pull/2169) - Respect custom `Delayed::Job.max_attempts` if it's defined [#2176](https://github.com/getsentry/sentry-ruby/pull/2176) +- Allow non-string error message to be reported to sentry ([#2137](https://github.com/getsentry/sentry-ruby/pull/2137)) ## 5.13.0 diff --git a/sentry-ruby/lib/sentry/interfaces/single_exception.rb b/sentry-ruby/lib/sentry/interfaces/single_exception.rb index 73e75c812..96217c077 100644 --- a/sentry-ruby/lib/sentry/interfaces/single_exception.rb +++ b/sentry-ruby/lib/sentry/interfaces/single_exception.rb @@ -22,6 +22,7 @@ def initialize(exception:, stacktrace: nil) else exception.message || "" end + exception_message = exception_message.inspect unless exception_message.is_a?(String) @value = Utils::EncodingHelper.encode_to_utf_8(exception_message.byteslice(0..Event::MAX_MESSAGE_SIZE_IN_BYTES)) diff --git a/sentry-ruby/spec/sentry/client_spec.rb b/sentry-ruby/spec/sentry/client_spec.rb index 8ffc43a44..a3517dc1d 100644 --- a/sentry-ruby/spec/sentry/client_spec.rb +++ b/sentry-ruby/spec/sentry/client_spec.rb @@ -209,6 +209,18 @@ def sentry_context expect(hash[:exception][:values][0][:value]).to eq("undefined method `[]' for nil:NilClass") end end + + it "converts non-string error message" do + NonStringMessageError = Class.new(StandardError) do + def detailed_message(*) + { foo: "bar" } + end + end + + event = subject.event_from_exception(NonStringMessageError.new) + expect(event).to be_a(Sentry::ErrorEvent) + expect(Sentry::Event.get_message_from_exception(event.to_hash)).to match("NonStringMessageError: {:foo=>\"bar\"}") + end end it "sets threads interface without stacktrace" do