Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

build: modernize with "go fix -errorsastype": use errors.AsType

Nick Craig-Wood33e9251b5222
diff --git a/backend/azureblob/azureblob.go b/backend/azureblob/azureblob.go
index 6df71619c..a1c945110 100644
--- a/backend/azureblob/azureblob.go
+++ b/backend/azureblob/azureblob.go
@@ -576,8 +576,7 @@ func (f *Fs) shouldRetry(ctx context.Context, err error) (bool, error) {
if fserrors.ContextError(ctx, &err) {
return false, err
}
- var storageErr *azcore.ResponseError
- if errors.As(err, &storageErr) {
+ if storageErr, ok := errors.AsType[*azcore.ResponseError](err); ok {
// General errors from:
// https://learn.microsoft.com/en-us/rest/api/storageservices/common-rest-api-error-codes
// Blob specific errors from:
@@ -2796,8 +2795,7 @@ func (f *Fs) OpenChunkWriter(ctx context.Context, remote string, src fs.ObjectIn
// isInvalidBlockOrBlob looks for the InvalidBlockOrBlob error in err
// returning true if it is found
func isInvalidBlockOrBlob(err error) bool {
- var storageErr *azcore.ResponseError
- if errors.As(err, &storageErr) {
+ if storageErr, ok := errors.AsType[*azcore.ResponseError](err); ok {
return storageErr.ErrorCode == string(bloberror.InvalidBlobOrBlock)
}
return false
diff --git a/backend/gofile/gofile.go b/backend/gofile/gofile.go
index fc48b7c59..87abca522 100644
--- a/backend/gofile/gofile.go
+++ b/backend/gofile/gofile.go
@@ -194,8 +194,7 @@ var retryErrorCodes = []int{
// Return true if the api error has the status given
func isAPIErr(err error, status string) bool {
- var apiErr api.Error
- if errors.As(err, &apiErr) {
+ if apiErr, ok := errors.AsType[api.Error](err); ok {
return apiErr.Status == status
}
return false
diff --git a/backend/internxt/internxt.go b/backend/internxt/internxt.go
index 0690e88b5..aa22e3fe1 100644
--- a/backend/internxt/internxt.go
+++ b/backend/internxt/internxt.go
@@ -54,8 +54,7 @@ func (f *Fs) shouldRetry(ctx context.Context, err error) (bool, error) {
if fserrors.ContextError(ctx, &err) {
return false, err
}
- var httpErr *sdkerrors.HTTPError
- if errors.As(err, &httpErr) {
+ if httpErr, ok := errors.AsType[*sdkerrors.HTTPError](err); ok {
switch httpErr.StatusCode() {
case 401:
if !f.authFailed {
@@ -1064,8 +1063,7 @@ func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, op
err := o.f.pacer.Call(func() (bool, error) {
err := files.DeleteFile(ctx, o.f.cfg, backupUUID)
if err != nil {
- var httpErr *sdkerrors.HTTPError
- if errors.As(err, &httpErr) {
+ if httpErr, ok := errors.AsType[*sdkerrors.HTTPError](err); ok {
// Treat 404 (Not Found) and 204 (No Content) as success
switch httpErr.StatusCode() {
case 404, 204:
@@ -1117,8 +1115,7 @@ func isEmptyFileLimitError(err error) bool {
// fileTooLargeError extracts the SDK's FileTooLargeError from a wrapped error
// chain, returning it (or nil) so callers can branch on the size limit.
func fileTooLargeError(err error) *sdkerrors.FileTooLargeError {
- var tooLarge *sdkerrors.FileTooLargeError
- if errors.As(err, &tooLarge) {
+ if tooLarge, ok := errors.AsType[*sdkerrors.FileTooLargeError](err); ok {
return tooLarge
}
return nil
diff --git a/backend/linkbox/linkbox.go b/backend/linkbox/linkbox.go
index 04e481248..a91eb8857 100644
--- a/backend/linkbox/linkbox.go
+++ b/backend/linkbox/linkbox.go
@@ -1019,8 +1019,7 @@ func (f *Fs) shouldRetry(ctx context.Context, resp *http.Response, err error) (b
// intermittently returns an HTML challenge page with a 200
// status instead of JSON. This surfaces as a JSON syntax error,
// so retry it to let the pacer back off until the block lifts.
- var syntaxErr *json.SyntaxError
- if errors.As(err, &syntaxErr) {
+ if _, ok := errors.AsType[*json.SyntaxError](err); ok {
return true, err
}
return fserrors.ShouldRetry(err) || fserrors.ShouldRetryHTTP(resp, retryErrorCodes), err
diff --git a/backend/pikpak/pikpak.go b/backend/pikpak/pikpak.go
index d0cd0b921..59d10d294 100644
--- a/backend/pikpak/pikpak.go
+++ b/backend/pikpak/pikpak.go
@@ -432,8 +432,7 @@ func (f *Fs) shouldRetry(ctx context.Context, resp *http.Response, err error) (b
// traceback to possible api.Error wrapped in err, and re-authorize if necessary
// "unauthenticated" (16): when access_token is invalid, but should be handled by oauthutil
- var terr *oauth2.RetrieveError
- if errors.As(err, &terr) {
+ if terr, ok := errors.AsType[*oauth2.RetrieveError](err); ok {
apiErr := new(api.Error)
if err := json.Unmarshal(terr.Body, apiErr); err == nil {
if apiErr.Reason == "invalid_grant" {
diff --git a/backend/protondrive/protondrive.go b/backend/protondrive/protondrive.go
index 142bec421..54f40d81e 100644
--- a/backend/protondrive/protondrive.go
+++ b/backend/protondrive/protondrive.go
@@ -262,8 +262,7 @@ func shouldRetry(ctx context.Context, err error) (bool, error) {
if err == nil {
return false, nil
}
- var apiErr *proton.APIError
- if errors.As(err, &apiErr) {
+ if apiErr, ok := errors.AsType[*proton.APIError](err); ok {
// Code 200501 is a generic Drive operation-failure code. Proton also
// returns it with an HTTP 422 for permanent validation failures (for
// example a content key packet that cannot be verified, or an upload
diff --git a/backend/s3/s3.go b/backend/s3/s3.go
index 94de158b1..44b312d0f 100644
--- a/backend/s3/s3.go
+++ b/backend/s3/s3.go
@@ -1279,8 +1279,7 @@ func (f *Fs) shouldRetry(ctx context.Context, err error) (bool, error) {
}
// https://github.com/aws/aws-sdk-go-v2/blob/main/CHANGELOG.md#error-handling
// If this is an awserr object, try and extract more useful information to determine if we should retry
- var awsError smithy.APIError
- if errors.As(err, &awsError) {
+ if awsError, ok := errors.AsType[smithy.APIError](err); ok {
// Simple case, check the original embedded error in case it's generically retryable
if fserrors.ShouldRetry(awsError) {
return true, err
@@ -2507,8 +2506,7 @@ func (f *Fs) list(ctx context.Context, opt listOpt, fn listFn) error {
listBucket.URLEncodeListings(urlEncodeListings)
resp, versionIDs, err = listBucket.List(ctx)
if err != nil && !urlEncodeListings {
- var xmlErr *xml.SyntaxError
- if errors.As(err, &xmlErr) {
+ if _, ok := errors.AsType[*xml.SyntaxError](err); ok {
// Retry the listing with URL encoding as there were characters that XML can't encode
urlEncodeListings = true
fs.Debugf(f, "Retrying listing because of characters which can't be XML encoded")
@@ -2945,8 +2943,7 @@ func (f *Fs) makeBucket(ctx context.Context, bucket string) error {
if err == nil {
fs.Infof(f, "Bucket %q created with ACL %q", bucket, f.opt.BucketACL)
}
- var awsErr smithy.APIError
- if errors.As(err, &awsErr) {
+ if awsErr, ok := errors.AsType[smithy.APIError](err); ok {
switch awsErr.ErrorCode() {
case "BucketAlreadyOwnedByYou":
err = nil
@@ -4415,8 +4412,7 @@ func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.Read
resp, err = o.fs.c.GetObject(ctx, &req, s3.WithAPIOptions(APIOptions...))
return o.fs.shouldRetry(ctx, err)
})
- var awsError smithy.APIError
- if errors.As(err, &awsError) {
+ if awsError, ok := errors.AsType[smithy.APIError](err); ok {
if awsError.ErrorCode() == "InvalidObjectState" {
return nil, fmt.Errorf("Object in GLACIER, restore first: bucket=%q, key=%q", bucket, bucketPath)
}
diff --git a/backend/s3/s3_internal_test.go b/backend/s3/s3_internal_test.go
index 1a719d899..5ed209b63 100644
--- a/backend/s3/s3_internal_test.go
+++ b/backend/s3/s3_internal_test.go
@@ -496,10 +496,9 @@ func (f *Fs) InternalTestVersions(t *testing.T) {
return f.shouldRetry(ctx, err)
})
var errString string
- var awsError smithy.APIError
if err == nil {
errString = "No Error"
- } else if errors.As(err, &awsError) {
+ } else if awsError, ok := errors.AsType[smithy.APIError](err); ok {
errString = awsError.ErrorCode()
} else {
assert.Fail(t, "Unknown error %T %v", err, err)
diff --git a/cmd/serve/dlna/dlna.go b/cmd/serve/dlna/dlna.go
index 417ca003c..752107270 100644
--- a/cmd/serve/dlna/dlna.go
+++ b/cmd/serve/dlna/dlna.go
@@ -307,8 +307,7 @@ func (s *server) serviceControlHandler(w http.ResponseWriter, r *http.Request) {
var env soap.Envelope
r.Body = http.MaxBytesReader(w, r.Body, maxSOAPBodySize)
if err := xml.NewDecoder(r.Body).Decode(&env); err != nil {
- var maxBytesErr *http.MaxBytesError
- if errors.As(err, &maxBytesErr) {
+ if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
http.Error(w, "SOAP request body too large", http.StatusRequestEntityTooLarge)
return
}