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

Add HTTPCode to APIError #229

Closed
tritone opened this issue Sep 27, 2022 · 2 comments · Fixed by #274
Closed

Add HTTPCode to APIError #229

tritone opened this issue Sep 27, 2022 · 2 comments · Fixed by #274
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@tritone
Copy link

tritone commented Sep 27, 2022

APIError was intended to be transport-agnostic, so it currently doesn't have any fields for HTTP or gRPC response codes. However, will lead to some clunky error handling code for users once we use APIError on apiary library calls. To extract the most important fields, users will have to call errors.As twice with two separate error types:

if err := bucket.Create(ctx, "bad-project-id", nil); err != nil {
        var aErr *googleapi.Error
        if errors.As(err, &aErr) {
                fmt.Printf("Status: %d\n", aErr.Code)  // pulls HTTP response code
        }
        var apiErr *apierror.APIError
        if errors.As(err, &apiErr) {
                fmt.Printf("Reason: %s\n", apiErr.Reason())  // pulls error reason
                fmt.Printf("Details: %sn", apiErr.Details())
        }
        // Handle error
}

Code is in fact the only field from googleapi.Error that we are currently missing out on in APIError. For manual clients such as storage where most users will be HTTP-only, it would be cleaner and easier to document if we could add a method to retrieve this into APIError as well.

It may also be nice to pull the gRPC status into APIError as well; however, I think it's preferable to keep these as separate methods (HTTPCode and GRPCCode say) in order to avoid having to map between HTTP and gRPC statuses.

@tritone tritone added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p3 Desirable enhancement or fix. May not be included in next release. labels Sep 27, 2022
@cecobask
Copy link

cecobask commented Nov 23, 2022

+1 on this feature request

@ribrdb
Copy link

ribrdb commented Feb 16, 2023

It seems like the json errors should include a grpc status code too, but the code is currently ignoring it:
https://pkg.go.dev/github.com/googleapis/gax-go/v2/apierror/internal/proto#Error_Status

It seems like the apierror should return that, or unknown if it's not included. Instead APIError.GRPCStatus() is returning nil for http errors, which means "OK".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants