From 42c0adc47ae659a63ce823f7c62a02e4c78d121e Mon Sep 17 00:00:00 2001 From: Manoj M J Date: Fri, 8 Feb 2019 13:54:52 +0530 Subject: [PATCH 1/4] If a custom object responds to `id` method, show the id value, instead of showing "[OBJECT]" --- lib/bugsnag/cleaner.rb | 8 +++++++- spec/cleaner_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/bugsnag/cleaner.rb b/lib/bugsnag/cleaner.rb index 25ca20508..bbe535c92 100644 --- a/lib/bugsnag/cleaner.rb +++ b/lib/bugsnag/cleaner.rb @@ -7,6 +7,7 @@ class Cleaner RECURSION = '[RECURSION]'.freeze OBJECT = '[OBJECT]'.freeze RAISED = '[RAISED]'.freeze + OBJECT_WITH_ID_AND_CLASS = '[OBJECT]: [Class]: %{class_name} [ID]: %{id}'.freeze ## # @param configuration [Configuration] @@ -121,7 +122,12 @@ def traverse_object(obj, seen, scope) # avoid leaking potentially sensitive data from objects' #inspect output if str =~ /#<.*>/ - OBJECT + # Use id of the object if available + if obj.respond_to?(:id) + OBJECT_WITH_ID_AND_CLASS % { class_name: obj.class, id: obj.id } + else + OBJECT + end else clean_string(str) end diff --git a/spec/cleaner_spec.rb b/spec/cleaner_spec.rb index 3f207a0e7..60a4c65ce 100644 --- a/spec/cleaner_spec.rb +++ b/spec/cleaner_spec.rb @@ -134,6 +134,16 @@ def to_s expect(subject.clean_object(object)).to eq("[RECURSION]") end + it "cleans custom objects to show the id of the object if object responds to id method" do + class Macaron + def id + 10 + end + end + a = Macaron.new + expect(subject.clean_object(a)).to eq("[OBJECT]: [Class]: #{a.class.name} [ID]: #{a.id}") + end + it "cleans up binary strings properly" do if RUBY_VERSION > "1.9" obj = "Andr\xc7\xff" From eb030386facdf7a2613c7c6ef7a77e33098d8903 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Wed, 15 Jul 2020 16:59:02 +0100 Subject: [PATCH 2/4] Add changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 608f7027f..a8fbd7978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ Changelog | [#602](https://github.com/bugsnag/bugsnag-ruby/pull/602) | [#603](https://github.com/bugsnag/bugsnag-ruby/pull/603) +* If a custom object responds to `id` method, show the id and class in error reports + | [#531](https://github.com/bugsnag/bugsnag-ruby/pull/531) + | [manojmj92](https://github.com/manojmj92) + ### Deprecated * The `ignore_classes` configuration option has been deprecated in favour of `discard_classes`. `ignore_classes` will be removed in the next major release From e74728bd8d3092c6bd0742f1d12c2da1244aa8de Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Wed, 15 Jul 2020 17:42:52 +0100 Subject: [PATCH 3/4] Make Rubocop happy --- lib/bugsnag/cleaner.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/bugsnag/cleaner.rb b/lib/bugsnag/cleaner.rb index bbe535c92..b33d06600 100644 --- a/lib/bugsnag/cleaner.rb +++ b/lib/bugsnag/cleaner.rb @@ -7,7 +7,7 @@ class Cleaner RECURSION = '[RECURSION]'.freeze OBJECT = '[OBJECT]'.freeze RAISED = '[RAISED]'.freeze - OBJECT_WITH_ID_AND_CLASS = '[OBJECT]: [Class]: %{class_name} [ID]: %{id}'.freeze + OBJECT_WITH_ID_AND_CLASS = '[OBJECT]: [Class]: %s [ID]: %d'.freeze ## # @param configuration [Configuration] @@ -124,7 +124,7 @@ def traverse_object(obj, seen, scope) if str =~ /#<.*>/ # Use id of the object if available if obj.respond_to?(:id) - OBJECT_WITH_ID_AND_CLASS % { class_name: obj.class, id: obj.id } + format(OBJECT_WITH_ID_AND_CLASS, class_name: obj.class, id: obj.id) else OBJECT end From cb14be546c139eb70234d5a10f585c3f9b3bd622 Mon Sep 17 00:00:00 2001 From: Joe Haines Date: Wed, 15 Jul 2020 17:43:18 +0100 Subject: [PATCH 4/4] Fix test failures in Ruby 1.9 --- spec/cleaner_spec.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/cleaner_spec.rb b/spec/cleaner_spec.rb index 60a4c65ce..96bb9dfec 100644 --- a/spec/cleaner_spec.rb +++ b/spec/cleaner_spec.rb @@ -135,12 +135,13 @@ def to_s end it "cleans custom objects to show the id of the object if object responds to id method" do - class Macaron + class MacaronWithId def id 10 end end - a = Macaron.new + + a = MacaronWithId.new expect(subject.clean_object(a)).to eq("[OBJECT]: [Class]: #{a.class.name} [ID]: #{a.id}") end