Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mvo5
Copy link
Contributor

@mvo5 mvo5 commented May 8, 2024

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 into clienterrors.Error.Details the details get lost because the json.Marshaler will not know how to handler an
error 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:

  1. make clienterrors.Error a real error type, it's kinda unusual that an error does not implement the error interface.
  2. Rethink 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.

@mvo5 mvo5 requested a review from schuellerf May 8, 2024 12:54
@mvo5 mvo5 force-pushed the test-clientdetails branch 3 times, most recently from 07ceb4c to bba6087 Compare May 8, 2024 13:30
func TestErrorJSONMarshal(t *testing.T) {
err := fmt.Errorf("some-error")

json, err := json.Marshal(clienterrors.WorkerClientError(2, "details", err))
Copy link
Contributor

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.

Copy link
Contributor Author

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).

@mvo5 mvo5 force-pushed the test-clientdetails branch 2 times, most recently from 6fd78c2 to c7f6fdf Compare May 8, 2024 15:08
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)
Copy link
Contributor Author

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

Copy link
Contributor

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 :-)

schuellerf
schuellerf previously approved these changes May 8, 2024
Copy link
Contributor

@schuellerf schuellerf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks pretty complete

@mvo5 mvo5 requested a review from croissanne May 13, 2024 10:33
mvo5 added 4 commits May 13, 2024 15:00
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.
@mvo5 mvo5 changed the title clientdetails: add test for error reporting clientdetails: handle error interfaces in Details somewhat May 13, 2024
Copy link
Member

@croissanne croissanne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants