-
Notifications
You must be signed in to change notification settings - Fork 1
[BB-1538] Wrap GitHub API Errors with Grpc Codes #108
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,10 +11,12 @@ import ( | |
| v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" | ||
| "github.com/conductorone/baton-sdk/pkg/annotations" | ||
| "github.com/conductorone/baton-sdk/pkg/pagination" | ||
| "github.com/conductorone/baton-sdk/pkg/uhttp" | ||
| "github.com/google/go-github/v69/github" | ||
| "github.com/shurcooL/githubv4" | ||
| "golang.org/x/text/cases" | ||
| "golang.org/x/text/language" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/protobuf/types/known/timestamppb" | ||
| ) | ||
|
|
||
|
|
@@ -249,3 +251,23 @@ func isPermissionError(resp *github.Response) bool { | |
| } | ||
| return resp.StatusCode == http.StatusForbidden | ||
| } | ||
|
|
||
| // wrapGitHubError wraps GitHub API errors with appropriate gRPC status codes based on the HTTP response. | ||
| // It handles rate limiting, authentication errors, permission errors, and generic errors. | ||
| // The contextMsg parameter should describe the operation that failed (e.g., "failed to list teams"). | ||
| func wrapGitHubError(err error, resp *github.Response, contextMsg string) error { | ||
| if err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| if isRatelimited(resp) { | ||
| return uhttp.WrapErrors(codes.Unavailable, "too many requests", err) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't seem right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like if its rate limited.... shouldn't we be returning that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pquerna this is wrapping the error with the status code so baton-sdk will wait and retry There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want the rate limit metadata?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do and it seems like the GitHub go SDK does expose it. I have filed a separate Jira issue for it https://conductorone.atlassian.net/browse/BB-1660 |
||
| } | ||
| if isAuthError(resp) { | ||
| return uhttp.WrapErrors(codes.Unauthenticated, contextMsg, err) | ||
| } | ||
| if isPermissionError(resp) { | ||
| return uhttp.WrapErrors(codes.PermissionDenied, contextMsg, err) | ||
| } | ||
| return fmt.Errorf("%s: %w", contextMsg, 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.
We could add the rate limit here from the error as per https://conductorone.atlassian.net/browse/BB-1660 or we could do it in a separate PR
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, it is best to do a specific PR to resolve that