Skip to content

Commit

Permalink
Merge pull request #605 from bugsnag/use-id
Browse files Browse the repository at this point in the history
If a custom object responds to `id` method, show the id and class value, instead of showing "[OBJECT]" in error reports
  • Loading branch information
imjoehaines committed Jul 16, 2020
2 parents 2915ced + cb14be5 commit 88cbcc3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/bugsnag/cleaner.rb
Expand Up @@ -7,6 +7,7 @@ class Cleaner
RECURSION = '[RECURSION]'.freeze
OBJECT = '[OBJECT]'.freeze
RAISED = '[RAISED]'.freeze
OBJECT_WITH_ID_AND_CLASS = '[OBJECT]: [Class]: %<class_name>s [ID]: %<id>d'.freeze

##
# @param configuration [Configuration]
Expand Down Expand Up @@ -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)
format(OBJECT_WITH_ID_AND_CLASS, class_name: obj.class, id: obj.id)
else
OBJECT
end
else
clean_string(str)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/cleaner_spec.rb
Expand Up @@ -134,6 +134,17 @@ 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 MacaronWithId
def id
10
end
end

a = MacaronWithId.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"
Expand Down

0 comments on commit 88cbcc3

Please sign in to comment.