-
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.
This ensures all error-related data is properly encoded.
- Loading branch information
Showing
12 changed files
with
116 additions
and
47 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,38 @@ | ||
# 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, message, backtrace = []) | ||
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(nil, nil) | ||
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
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,58 @@ | ||
require 'helper' | ||
require 'ddtrace/error' | ||
|
||
module Datadog | ||
class ErrorTest < Minitest::Test | ||
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(nil, nil) | ||
|
||
assert_equal('', error.type) | ||
assert_equal('', error.message) | ||
assert_equal('', 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(nil, nil) | ||
|
||
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_equal('', 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_equal('', error.backtrace) | ||
end | ||
end | ||
end |