-
Notifications
You must be signed in to change notification settings - Fork 107
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
clientdetails: handle error interfaces in Details
somewhat
#4137
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9160dc3
clientdetails: add test for error reporting
mvo5 4165bf4
clienterrors: add (failing) test to reproduce error interface handling
mvo5 d4300fc
clienterrors: add (one possible) way to fix the missing errors
mvo5 7a862d4
clienterrors: detect and error on deeply nested errors
mvo5 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package clienterrors_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/osbuild/osbuild-composer/internal/worker/clienterrors" | ||
) | ||
|
||
type customErr struct{} | ||
|
||
func (ce *customErr) Error() string { | ||
return "customErr" | ||
} | ||
|
||
func TestErrorInterface(t *testing.T) { | ||
for _, tc := range []struct { | ||
err error | ||
expectedStr string | ||
}{ | ||
{fmt.Errorf("some error"), "some error"}, | ||
{&customErr{}, "customErr"}, | ||
} { | ||
wce := clienterrors.WorkerClientError(2, "reason", tc.err) | ||
assert.Equal(t, fmt.Sprintf("Code: 2, Reason: reason, Details: %s", tc.expectedStr), wce.String()) | ||
} | ||
} | ||
|
||
func TestErrorJSONMarshal(t *testing.T) { | ||
for _, tc := range []struct { | ||
err interface{} | ||
expectedStr string | ||
}{ | ||
{fmt.Errorf("some-error"), `"some-error"`}, | ||
{[]error{fmt.Errorf("err1"), fmt.Errorf("err2")}, `["err1","err2"]`}, | ||
{"random detail", `"random detail"`}, | ||
} { | ||
json, err := json.Marshal(clienterrors.WorkerClientError(2, "reason", tc.err)) | ||
assert.NoError(t, err) | ||
assert.Equal(t, fmt.Sprintf(`{"id":2,"reason":"reason","details":%s}`, tc.expectedStr), string(json)) | ||
} | ||
} | ||
|
||
func TestErrorJSONMarshalDetectsNestedErrs(t *testing.T) { | ||
details := struct { | ||
Unrelated string | ||
NestedErr error | ||
Nested struct { | ||
DeepErr error | ||
} | ||
}{ | ||
Unrelated: "unrelated", | ||
NestedErr: fmt.Errorf("some-nested-error"), | ||
Nested: struct { | ||
DeepErr error | ||
}{ | ||
DeepErr: fmt.Errorf("deep-err"), | ||
}, | ||
} | ||
_, err := json.Marshal(clienterrors.WorkerClientError(2, "reason", details)) | ||
assert.Equal(t, `json: error calling MarshalJSON for type *clienterrors.Error: found nested error in {Unrelated:unrelated NestedErr:some-nested-error Nested:{DeepErr:deep-err}}: some-nested-error`, err.Error()) | ||
} |
Oops, something went wrong.
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.
@mvo5 Do I get this right, that only the first will be returned (same for array and map)? Is this on purpose?
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.
Yes, only the first is detected right now. We can change that of course, I'm not sure if it's worth it (also I'm not sure this entire PR is the right approach, it's nice in the sense that it does detect the issue now but it still will not catch other cases where we pass something to
Details interface{}
that is in fact an interface that is not properly serializable to jsonI still wonder if we a better appraoch would be:
a) merge #4145 to fix the immedidate issue
b) change
Details inteface{}
->Details string
orDetails []string
orDetails json.RawMessage
(the later would force the caller to serialize). (i.e. ensure passing an err becomes a compile time error instead of a runtime error).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.
Not sure where the idea comes from that Details should be flexible but then it would be a) and b) with
json.RawMessage
Starting with #4145 for now sound perfect 😏
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.
it's probably worth to salvage (some of) the test cases from this PR but otherwise I'm fine closing it (or closing it after (a) and (b) got done). [fwiw, (b) with json.RawMessage might become a bit annoying on the caller side, but I guess we need to try it to see how good/bad it will be :)