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

fix(v2/apierror): fix (*APIError).Error() for unwrapped Status #351

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion v2/apierror/apierror.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ func (a *APIError) Error() string {
// an ugly way.
msg = fmt.Sprintf("googleapi: Error %d: %s", a.httpErr.Code, a.httpErr.Message)
} else if a.status != nil {
msg = a.err.Error()
if a.err != nil {
quartzmo marked this conversation as resolved.
Show resolved Hide resolved
msg = a.err.Error()
} else {
msg = a.status.Message()
}
}
return strings.TrimSpace(fmt.Sprintf("%s\n%s", msg, a.details))
}
Expand Down
48 changes: 37 additions & 11 deletions v2/apierror/apierror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,30 +465,56 @@ func TestParseError(t *testing.T) {
se := errors.New("standard error")

tests := []struct {
source error
apierr *APIError
b bool
name string
source error
wantErr *APIError
wantParsed bool
}{
{hae, &APIError{httpErr: hae, status: haeS, details: ErrDetails{ErrorInfo: httpErrInfo}}, true},
{se, &APIError{err: se}, false},
{
name: "with http ErrorInfo details",
source: hae,
wantErr: &APIError{httpErr: hae, status: haeS, details: ErrDetails{ErrorInfo: httpErrInfo}},
wantParsed: true,
},
{
name: "standard error no details",
source: se,
wantErr: &APIError{err: se},
wantParsed: false,
},
{
name: "grpc status no details",
source: haeS.Err(),
wantErr: &APIError{httpErr: hae, status: haeS, details: ErrDetails{}},
wantParsed: true,
},
}

for _, tc := range tests {
// ParseError with wrap = true is covered by TestFromError, above.
got, apiB := ParseError(tc.source, false)
if tc.b != apiB {
t.Errorf("ParseError(%s, false): got %v, want %v", tc.apierr, apiB, tc.b)
got, gotParsed := ParseError(tc.source, false)
if tc.wantParsed != gotParsed {
t.Errorf("ParseError(%s, false): got %v, want %v", tc.wantErr, gotParsed, tc.wantParsed)
}
if tc.b {
if diff := cmp.Diff(got.details, tc.apierr.details, cmp.Comparer(proto.Equal)); diff != "" {
if got != nil {
if got.Error() == "" {
t.Errorf("got.Error(): got %q, want non-empty", got.Error())
}
}
if tc.wantParsed {
if diff := cmp.Diff(got.details, tc.wantErr.details, cmp.Comparer(proto.Equal)); diff != "" {
t.Errorf("got(-), want(+),: \n%s", diff)
}
if diff := cmp.Diff(got.status, tc.apierr.status, cmp.Comparer(proto.Equal), cmp.AllowUnexported(status.Status{})); diff != "" {
if diff := cmp.Diff(got.status, tc.wantErr.status, cmp.Comparer(proto.Equal), cmp.AllowUnexported(status.Status{})); diff != "" {
t.Errorf("got(-), want(+),: \n%s", diff)
}
if got.err != nil {
t.Errorf("got %s, want nil", got.err)
}
} else {
if got != nil {
t.Errorf("got %s, want nil", got)
}
}
}
if err, _ := ParseError(nil, false); err != nil {
Expand Down