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
base: main
Are you sure you want to change the base?
Conversation
07ceb4c
to
bba6087
Compare
func TestErrorJSONMarshal(t *testing.T) { | ||
err := fmt.Errorf("some-error") | ||
|
||
json, err := json.Marshal(clienterrors.WorkerClientError(2, "details", err)) |
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.
I think I saw a usage of .Details
being of type []error
but that can get arbitrary complex I guess :-/
maybe we can just cover this as a second use case being marshaled correctly.
This all boils down to the problem that .Details
should be "arbitrary"…
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
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.
I pushed a bit of a compromise, needs more tests as it does not test for all the ensureNoErrs
cases and also bails on the first nested error (but prints the whole structure so it should be okay).
6fd78c2
to
c7f6fdf
Compare
details = fmt.Sprintf("%v", v) | ||
default: | ||
if err := ensureNoErrs(reflect.ValueOf(v)); err != nil { | ||
return nil, fmt.Errorf("found nested error in %+v: %v", e.Details, err) |
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 is probably a bit too terse, maybe found nested error in Details field
or something
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.
For the rare cases (none?) where this will happen, I think it's good :-)
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.
looks pretty complete
We have no tests for the clienterror, start with a trivial one.
Florian pointed out that `clienterror.Error.Details` could contain arbitrarily deeply nested error values. Add code to detect this. Also deal with the (common?) case that `.Details` contains a list of errors.
Details
somewhat
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.
thank you 😍
The clienterror report has no tests right now, that is not ideal so this PR adds some.
While doing that it fixes an issue that @schuellerf discovered, i.e. that when an
error
interface is passed intoclienterrors.Error.Details
the details get lost because the json.Marshaler will not know how to handler anerror interface.
So I think this is fine to merge and will improve the current state. However I think we should rework the
clienterrors.Error so that go types will help us detect issues at compile time. With this PR we will only
detect nested errorrs at runtime and error and it will not deal with nested interface of other types.
We should probably:
clienterrors.Error
a real error type, it's kinda unusual that an error does not implement theerror
interface.Details
- AFAICT all the details will be "flattended" (via%v
) after the de-serialization anyway, so we might just make details a "string". Of course testing this requires a bit of digging into the source and maybe writting some tests that validate that we indeed just use do the flattening on the other side.