Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion example/basicauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"bufio"
"context"
"errors"
"fmt"
"os"
"strings"
Expand All @@ -43,7 +44,7 @@ func main() {
user, _, err := client.Users.Get(ctx, "")

// Is this a two-factor auth error? If so, prompt for OTP and try again.
if _, ok := err.(*github.TwoFactorAuthError); ok {
if errors.As(err, new(*github.TwoFactorAuthError)) {
fmt.Print("\nGitHub OTP: ")
otp, _ := r.ReadString('\n')
tp.OTP = strings.TrimSpace(otp)
Expand Down
3 changes: 2 additions & 1 deletion github/actions_workflow_runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package github
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -482,7 +483,7 @@ func TestActionsService_CancelWorkflowRunByID(t *testing.T) {

ctx := context.Background()
resp, err := client.Actions.CancelWorkflowRunByID(ctx, "o", "r", 3434)
if _, ok := err.(*AcceptedError); !ok {
if !errors.As(err, new(*AcceptedError)) {
t.Errorf("Actions.CancelWorkflowRunByID returned error: %v (want AcceptedError)", err)
}
if resp.StatusCode != http.StatusAccepted {
Expand Down
43 changes: 23 additions & 20 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,8 @@ func (c *Client) bareDo(ctx context.Context, caller *http.Client, req *http.Requ
}

// If the error type is *url.Error, sanitize its URL before returning.
if e, ok := err.(*url.Error); ok {
var e *url.Error
if errors.As(err, &e) {
if url, err := url.Parse(e.URL); err == nil {
e.URL = sanitizeURL(url).String()
return response, e
Expand All @@ -923,8 +924,8 @@ func (c *Client) bareDo(ctx context.Context, caller *http.Client, req *http.Requ
// added to the AcceptedError and returned.
//
// Issue #1022
aerr, ok := err.(*AcceptedError)
if ok {
var aerr *AcceptedError
if errors.As(err, &aerr) {
b, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return response, readErr
Expand All @@ -934,8 +935,9 @@ func (c *Client) bareDo(ctx context.Context, caller *http.Client, req *http.Requ
err = aerr
}

rateLimitError, ok := err.(*RateLimitError)
if ok && req.Context().Value(SleepUntilPrimaryRateLimitResetWhenRateLimited) != nil {
var rateLimitError *RateLimitError
if errors.As(err, &rateLimitError) &&
req.Context().Value(SleepUntilPrimaryRateLimitResetWhenRateLimited) != nil {
if err := sleepUntilResetWithBuffer(req.Context(), rateLimitError.Rate.Reset.Time); err != nil {
return response, err
}
Expand All @@ -944,8 +946,8 @@ func (c *Client) bareDo(ctx context.Context, caller *http.Client, req *http.Requ
}

// Update the secondary rate limit if we hit it.
rerr, ok := err.(*AbuseRateLimitError)
if ok && rerr.RetryAfter != nil {
var rerr *AbuseRateLimitError
if errors.As(err, &rerr) && rerr.RetryAfter != nil {
// if a max duration is specified, make sure that we are waiting at most this duration
if c.MaxSecondaryRateLimitRetryAfterDuration > 0 && rerr.GetRetryAfter() > c.MaxSecondaryRateLimitRetryAfterDuration {
rerr.RetryAfter = &c.MaxSecondaryRateLimitRetryAfterDuration
Expand Down Expand Up @@ -992,8 +994,8 @@ var errInvalidLocation = errors.New("invalid or empty Location header in redirec
func (c *Client) bareDoUntilFound(ctx context.Context, req *http.Request, maxRedirects int) (*url.URL, *Response, error) {
response, err := c.bareDoIgnoreRedirects(ctx, req)
if err != nil {
rerr, ok := err.(*RedirectionError)
if ok {
var rerr *RedirectionError
if errors.As(err, &rerr) {
// If we receive a 302, transform potential relative locations into absolute and return it.
if rerr.StatusCode == http.StatusFound {
if rerr.Location == nil {
Expand Down Expand Up @@ -1181,8 +1183,8 @@ func (r *ErrorResponse) Error() string {

// Is returns whether the provided error equals this error.
func (r *ErrorResponse) Is(target error) bool {
v, ok := target.(*ErrorResponse)
if !ok {
var v *ErrorResponse
if !errors.As(target, &v) {
return false
}

Expand Down Expand Up @@ -1246,8 +1248,8 @@ func (r *RateLimitError) Error() string {

// Is returns whether the provided error equals this error.
func (r *RateLimitError) Is(target error) bool {
v, ok := target.(*RateLimitError)
if !ok {
var v *RateLimitError
if !errors.As(target, &v) {
return false
}

Expand All @@ -1273,8 +1275,8 @@ func (*AcceptedError) Error() string {

// Is returns whether the provided error equals this error.
func (ae *AcceptedError) Is(target error) bool {
v, ok := target.(*AcceptedError)
if !ok {
var v *AcceptedError
if !errors.As(target, &v) {
return false
}
return bytes.Equal(ae.Raw, v.Raw)
Expand All @@ -1300,8 +1302,8 @@ func (r *AbuseRateLimitError) Error() string {

// Is returns whether the provided error equals this error.
func (r *AbuseRateLimitError) Is(target error) bool {
v, ok := target.(*AbuseRateLimitError)
if !ok {
var v *AbuseRateLimitError
if !errors.As(target, &v) {
return false
}

Expand Down Expand Up @@ -1334,8 +1336,8 @@ func (r *RedirectionError) Error() string {

// Is returns whether the provided error equals this error.
func (r *RedirectionError) Is(target error) bool {
v, ok := target.(*RedirectionError)
if !ok {
var v *RedirectionError
if !errors.As(target, &v) {
return false
}

Expand Down Expand Up @@ -1486,7 +1488,8 @@ func parseBoolResponse(err error) (bool, error) {
return true, nil
}

if err, ok := err.(*ErrorResponse); ok && err.Response.StatusCode == http.StatusNotFound {
var rerr *ErrorResponse
if errors.As(err, &rerr) && rerr.Response.StatusCode == http.StatusNotFound {
// Simply false. In this one case, we do not pass the error through.
return false, nil
}
Expand Down
Loading
Loading