-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Further error-logging improvements #333
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,11 @@ import ( | |
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/zap/zapcore" | ||
|
||
richErrors "github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func BenchmarkBoolsArrayMarshaler(b *testing.B) { | ||
|
@@ -94,7 +97,11 @@ func TestArrayWrappers(t *testing.T) { | |
{"uint16s", Uint16s("", []uint16{1, 2}), []interface{}{uint16(1), uint16(2)}}, | ||
{"uint8s", Uint8s("", []uint8{1, 2}), []interface{}{uint8(1), uint8(2)}}, | ||
{"uintptrs", Uintptrs("", []uintptr{1, 2}), []interface{}{uintptr(1), uintptr(2)}}, | ||
{"errors", Errors("", []error{nil, errors.New("foo"), nil, errors.New("bar")}), []interface{}{"foo", "bar"}}, | ||
{ | ||
"errors", | ||
Errors("", []error{nil, errors.New("foo"), nil, errors.New("bar")}), | ||
[]interface{}{map[string]interface{}{"error": "foo"}, map[string]interface{}{"error": "bar"}}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add a test which also adds There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I can. Those code paths are already covered, but doesn't hurt to do it again. |
||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
|
@@ -105,3 +112,23 @@ func TestArrayWrappers(t *testing.T) { | |
assert.Equal(t, 1, len(enc.Fields), "%s: found extra keys in map: %v", tt.desc, enc.Fields) | ||
} | ||
} | ||
|
||
func TestErrorsArraysHandleRichErrors(t *testing.T) { | ||
errs := []error{richErrors.New("egad")} | ||
|
||
enc := zapcore.NewMapObjectEncoder() | ||
Errors("k", errs).AddTo(enc) | ||
assert.Equal(t, 1, len(enc.Fields), "Expected only top-level field.") | ||
|
||
val := enc.Fields["k"] | ||
arr, ok := val.([]interface{}) | ||
require.True(t, ok, "Expected top-level field to be an array.") | ||
require.Equal(t, 1, len(arr), "Expected only one error object in array.") | ||
|
||
serialized := arr[0] | ||
errMap, ok := serialized.(map[string]interface{}) | ||
require.True(t, ok, "Expected serialized error to be a map, got %T.", serialized) | ||
assert.Equal(t, "egad", errMap["error"], "Unexpected standard error string.") | ||
assert.Contains(t, errMap["errorVerbose"], "egad", "Verbose error string should be a superset of standard error.") | ||
assert.Contains(t, errMap["errorVerbose"], "TestErrorsArraysHandleRichErrors", "Verbose error string should contain a stacktrace.") | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this does mean our output for
[]error
ends up being a list of objects which have a keyerror
. I don't have strong opinions on this, but it does seem a little strange.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's definitely a little odd. I'm honestly not sure what else to do - it seems worse to have arrays that mix strings and objects. Would you prefer that? Or separate arrays of the errors and verbose errors?