Skip to content

Commit

Permalink
fix(rclone/operations): remove misleading permission error from Check…
Browse files Browse the repository at this point in the history
…Permissions

Assuming that every error is permission error is wrong and misleading.

Fixes #3738
  • Loading branch information
Michal-Leszczynski committed Mar 1, 2024
1 parent dae70b2 commit 13b83e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions pkg/rclone/operations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import (
"github.com/rclone/rclone/lib/pacer"
)

// PermissionError wraps remote fs errors returned by CheckPermissions function
// OperationError wraps remote fs errors returned by CheckPermissions function
// and allows to set a custom message returned to user.
type PermissionError struct {
type OperationError struct {
cause error
op string
statusCode int
}

func asPermissionError(op string, l fs.Fs, err error) PermissionError {
func asOperationError(op string, l fs.Fs, err error) OperationError {
statusCode := 400

if l.Name() == "s3" {
Expand All @@ -37,23 +37,23 @@ func asPermissionError(op string, l fs.Fs, err error) PermissionError {
}
}

return PermissionError{
return OperationError{
cause: err,
op: op,
statusCode: statusCode,
}
}

func (e PermissionError) Error() string {
return "no " + e.op + " permission" + ": " + e.cause.Error()
func (e OperationError) Error() string {
return "operation " + e.op + ": " + e.cause.Error()
}

func (e PermissionError) String() string {
func (e OperationError) String() string {
return e.Error()
}

// StatusCode returns HTTP status code that should be returned for this error.
func (e PermissionError) StatusCode() int {
func (e OperationError) StatusCode() int {
return e.statusCode
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func CheckPermissions(ctx context.Context, l fs.Fs) error {
if errors.Is(err, credentials.ErrNoValidProvidersFoundInChain) {
return errors.New("no providers - attach IAM Role to EC2 instance or put your access keys to s3 section of /etc/scylla-manager-agent/scylla-manager-agent.yaml and restart agent") // nolint: lll
}
return asPermissionError("put", l, err)
return asOperationError("put", l, err)
}
}

Expand All @@ -107,7 +107,7 @@ func CheckPermissions(ctx context.Context, l fs.Fs) error {
if err := operations.ListJSON(ctx, l, testDirName, &opts, func(item *operations.ListJSONItem) error {
return nil
}); err != nil {
return asPermissionError("list", l, err)
return asOperationError("list", l, err)
}
}

Expand All @@ -119,11 +119,11 @@ func CheckPermissions(ctx context.Context, l fs.Fs) error {
}
r, err := o.Open(ctx)
if err != nil {
return asPermissionError("open", l, err)
return asOperationError("open", l, err)
}
defer r.Close()
if _, err := io.Copy(io.Discard, r); err != nil {
return asPermissionError("copy", l, err)
return asOperationError("copy", l, err)
}
}

Expand All @@ -134,7 +134,7 @@ func CheckPermissions(ctx context.Context, l fs.Fs) error {
return errors.Wrap(err, "init remote temp dir")
}
if err := operations.Delete(ctx, f); err != nil {
return asPermissionError("delete", l, err)
return asOperationError("delete", l, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/rclone/rcserver/rcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (s Server) writeError(path string, in rc.Params, w http.ResponseWriter, err

fs.Errorf(nil, "rc: %q: error: %v", path, err)
// Adjust the error return for some well known errors
if e, ok := err.(operations.PermissionError); ok { // nolint: errorlint
if e, ok := err.(operations.OperationError); ok { // nolint: errorlint
status = e.StatusCode()
} else {
switch {
Expand Down

0 comments on commit 13b83e4

Please sign in to comment.