-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
127 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Datadog global namespace | ||
module Datadog | ||
# Error is a value-object responsible for sanitizing/encapsulating error data | ||
class Error | ||
attr_reader :type, :message, :backtrace | ||
|
||
def self.build_from(value) | ||
case value | ||
when Error then value | ||
when Array then new(*value) | ||
when Exception then new(value.class, value.message, value.backtrace) | ||
when ContainsMessage then new(value.class, value.message) | ||
else BlankError | ||
end | ||
end | ||
|
||
def initialize(type = nil, message = nil, backtrace = nil) | ||
backtrace = Array(backtrace).join("\n") | ||
@type = sanitize(type) | ||
@message = sanitize(message) | ||
@backtrace = sanitize(backtrace) | ||
end | ||
|
||
private | ||
|
||
def sanitize(value) | ||
value = value.to_s | ||
|
||
return value if value.encoding == ::Encoding::UTF_8 | ||
|
||
value.encode(::Encoding::UTF_8) | ||
end | ||
|
||
BlankError = Error.new | ||
ContainsMessage = ->(v) { v.respond_to?(:message) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
require 'helper' | ||
require 'ddtrace/error' | ||
|
||
module Datadog | ||
class ErrorTest < Minitest::Test | ||
CustomMessage = Struct.new(:message) | ||
|
||
def setup | ||
@error = Error.new('StandardError', 'message', %w[x y z]) | ||
end | ||
|
||
def test_type | ||
assert_equal('StandardError', @error.type) | ||
end | ||
|
||
def test_message | ||
assert_equal('message', @error.message) | ||
end | ||
|
||
def test_backtrace | ||
assert_equal("x\ny\nz", @error.backtrace) | ||
end | ||
|
||
def test_default_values | ||
error = Error.new | ||
|
||
assert_empty(error.type) | ||
assert_empty(error.message) | ||
assert_empty(error.backtrace) | ||
end | ||
|
||
# Empty strings were being interpreted as ASCII strings breaking `msgpack` | ||
# decoding on the agent-side. | ||
def test_enconding | ||
error = Datadog::Error.new | ||
|
||
assert_equal(::Encoding::UTF_8, error.type.encoding) | ||
assert_equal(::Encoding::UTF_8, error.message.encoding) | ||
assert_equal(::Encoding::UTF_8, error.backtrace.encoding) | ||
end | ||
|
||
def test_array_coercion | ||
error_payload = ['ZeroDivisionError', 'divided by 0'] | ||
error = Error.build_from(error_payload) | ||
|
||
assert_equal('ZeroDivisionError', error.type) | ||
assert_equal('divided by 0', error.message) | ||
assert_empty(error.backtrace) | ||
end | ||
|
||
def test_exception_coercion | ||
exception = ZeroDivisionError.new('divided by 0') | ||
error = Error.build_from(exception) | ||
|
||
assert_equal('ZeroDivisionError', error.type) | ||
assert_equal('divided by 0', error.message) | ||
assert_empty(error.backtrace) | ||
end | ||
|
||
def test_message_coercion | ||
message = CustomMessage.new('custom-message') | ||
error = Error.build_from(message) | ||
|
||
assert_equal('Datadog::ErrorTest::CustomMessage', error.type) | ||
assert_equal('custom-message', error.message) | ||
assert_empty(error.backtrace) | ||
end | ||
|
||
def test_nil_coercion | ||
error = Error.build_from(nil) | ||
|
||
assert_empty(error.type) | ||
assert_empty(error.message) | ||
assert_empty(error.backtrace) | ||
end | ||
end | ||
end |