Skip to content

Commit

Permalink
RUBY-991 handle Exceptions with non-String messages
Browse files Browse the repository at this point in the history
  • Loading branch information
benweint committed Jan 3, 2013
1 parent 84fb65c commit c59a6d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 8 additions & 0 deletions lib/new_relic/noticed_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def initialize(path, data, exception, timestamp = Time.now)
@message = (exception || '<no message>').to_s
end

unless @message.is_a?(String)
# In pre-1.9.3, Exception.new({}).to_s.class != String
# That is, Exception#to_s may not return a String instance if one wasn't
# passed in upon creation of the Exception. So, try to generate a useful
# String representation of the exception message, falling back to failsafe
@message = String(@message.inspect) rescue '<unknown message type>'
end

# clamp long messages to 4k so that we don't send a lot of
# overhead across the wire
@message = @message[0..4095] if @message.length > 4096
Expand Down
20 changes: 15 additions & 5 deletions test/new_relic/noticed_error_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
require File.expand_path(File.join(File.dirname(__FILE__),'..','test_helper'))

class NewRelic::Agent::NoticedErrorTest < Test::Unit::TestCase
def setup
@path = 'foo/bar/baz'
@params = { 'key' => 'val' }
@time = Time.now
end

def test_to_collector_array
time = Time.now
error = NewRelic::NoticedError.new('path', {'key' => 'val'},
Exception.new('test exception'), time)
e = Exception.new('test exception')
error = NewRelic::NoticedError.new(@path, @params, e, @time)
expected = [
(time.to_f * 1000).round, 'path', 'test exception', 'Exception',
{'key' => 'val'}
(@time.to_f * 1000).round, @path, 'test exception', 'Exception', @params
]
assert_equal expected, error.to_collector_array
end

def test_handles_non_string_exception_messages
e = Exception.new({ :non => :string })
error = NewRelic::NoticedError.new(@path, @params, e, @time)
assert_equal(String, error.message.class)
end
end

0 comments on commit c59a6d0

Please sign in to comment.