improve error wrapping

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino 2024-05-14 20:45:25 +02:00
parent 76fddc126d
commit 25a498a153
No known key found for this signature in database
GPG key ID: 935D2952DEC4EECF
5 changed files with 21 additions and 21 deletions

View file

@ -1610,7 +1610,7 @@ func (c *BaseConnection) GetOpUnsupportedError() error {
func getQuotaExceededError(protocol string) error { func getQuotaExceededError(protocol string) error {
switch protocol { switch protocol {
case ProtocolSFTP: case ProtocolSFTP:
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrQuotaExceeded.Error()) return fmt.Errorf("%w: %w", sftp.ErrSSHFxFailure, ErrQuotaExceeded)
case ProtocolFTP: case ProtocolFTP:
return ftpserver.ErrStorageExceeded return ftpserver.ErrStorageExceeded
default: default:
@ -1621,7 +1621,7 @@ func getQuotaExceededError(protocol string) error {
func getReadQuotaExceededError(protocol string) error { func getReadQuotaExceededError(protocol string) error {
switch protocol { switch protocol {
case ProtocolSFTP: case ProtocolSFTP:
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrReadQuotaExceeded.Error()) return fmt.Errorf("%w: %w", sftp.ErrSSHFxFailure, ErrReadQuotaExceeded)
default: default:
return ErrReadQuotaExceeded return ErrReadQuotaExceeded
} }
@ -1655,15 +1655,21 @@ func (c *BaseConnection) IsQuotaExceededError(err error) bool {
} }
} }
func isSFTPGoError(err error) bool {
return errors.Is(err, ErrPermissionDenied) || errors.Is(err, ErrNotExist) || errors.Is(err, ErrOpUnsupported) ||
errors.Is(err, ErrQuotaExceeded) || errors.Is(err, ErrReadQuotaExceeded) ||
errors.Is(err, vfs.ErrStorageSizeUnavailable) || errors.Is(err, ErrShuttingDown)
}
// GetGenericError returns an appropriate generic error for the connection protocol // GetGenericError returns an appropriate generic error for the connection protocol
func (c *BaseConnection) GetGenericError(err error) error { func (c *BaseConnection) GetGenericError(err error) error {
switch c.protocol { switch c.protocol {
case ProtocolSFTP: case ProtocolSFTP:
if err == vfs.ErrStorageSizeUnavailable { if errors.Is(err, vfs.ErrStorageSizeUnavailable) || errors.Is(err, ErrOpUnsupported) || errors.Is(err, sftp.ErrSSHFxOpUnsupported) {
return fmt.Errorf("%w: %v", sftp.ErrSSHFxOpUnsupported, err.Error()) return fmt.Errorf("%w: %w", sftp.ErrSSHFxOpUnsupported, err)
} }
if err == ErrShuttingDown { if isSFTPGoError(err) {
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, err.Error()) return fmt.Errorf("%w: %w", sftp.ErrSSHFxFailure, err)
} }
if err != nil { if err != nil {
var pathError *fs.PathError var pathError *fs.PathError
@ -1672,13 +1678,10 @@ func (c *BaseConnection) GetGenericError(err error) error {
return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, pathError.Op, pathError.Err.Error()) return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, pathError.Op, pathError.Err.Error())
} }
c.Log(logger.LevelError, "generic error: %+v", err) c.Log(logger.LevelError, "generic error: %+v", err)
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrGenericFailure.Error())
} }
return sftp.ErrSSHFxFailure return sftp.ErrSSHFxFailure
default: default:
if err == ErrPermissionDenied || err == ErrNotExist || err == ErrOpUnsupported || if isSFTPGoError(err) {
err == ErrQuotaExceeded || err == ErrReadQuotaExceeded || err == vfs.ErrStorageSizeUnavailable ||
err == ErrShuttingDown {
return err return err
} }
c.Log(logger.LevelError, "generic error: %+v", err) c.Log(logger.LevelError, "generic error: %+v", err)

View file

@ -217,16 +217,11 @@ func (t *BaseTransfer) SetCancelFn(cancelFn func()) {
// converts it into a more understandable form for the client if it is a // converts it into a more understandable form for the client if it is a
// well-known type of error // well-known type of error
func (t *BaseTransfer) ConvertError(err error) error { func (t *BaseTransfer) ConvertError(err error) error {
if t.Fs.IsNotExist(err) {
return t.Connection.GetNotExistError()
} else if t.Fs.IsPermission(err) {
return t.Connection.GetPermissionDeniedError()
}
var pathError *fs.PathError var pathError *fs.PathError
if errors.As(err, &pathError) { if errors.As(err, &pathError) {
return fmt.Errorf("%s %s: %s", pathError.Op, t.GetVirtualPath(), pathError.Err.Error()) return fmt.Errorf("%s %s: %s", pathError.Op, t.GetVirtualPath(), pathError.Err.Error())
} }
return err return t.Connection.GetFsError(t.Fs, err)
} }
// CheckRead returns an error if read if not allowed // CheckRead returns an error if read if not allowed

View file

@ -1794,7 +1794,7 @@ func TestTransferFailingReader(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
buf := make([]byte, 32) buf := make([]byte, 32)
_, err = transfer.ReadAt(buf, 0) _, err = transfer.ReadAt(buf, 0)
assert.EqualError(t, err, sftp.ErrSSHFxOpUnsupported.Error()) assert.ErrorIs(t, err, sftp.ErrSSHFxOpUnsupported)
if c, ok := transfer.(io.Closer); ok { if c, ok := transfer.(io.Closer); ok {
err = c.Close() err = c.Close()
assert.NoError(t, err) assert.NoError(t, err)
@ -1809,14 +1809,14 @@ func TestTransferFailingReader(t *testing.T) {
errRead := errors.New("read is not allowed") errRead := errors.New("read is not allowed")
tr := newTransfer(baseTransfer, nil, r, errRead) tr := newTransfer(baseTransfer, nil, r, errRead)
_, err = tr.ReadAt(buf, 0) _, err = tr.ReadAt(buf, 0)
assert.EqualError(t, err, errRead.Error()) assert.ErrorIs(t, err, sftp.ErrSSHFxFailure)
err = tr.Close() err = tr.Close()
assert.NoError(t, err) assert.NoError(t, err)
tr = newTransfer(baseTransfer, nil, nil, errRead) tr = newTransfer(baseTransfer, nil, nil, errRead)
_, err = tr.ReadAt(buf, 0) _, err = tr.ReadAt(buf, 0)
assert.EqualError(t, err, errRead.Error()) assert.ErrorIs(t, err, sftp.ErrSSHFxFailure)
err = tr.Close() err = tr.Close()
assert.NoError(t, err) assert.NoError(t, err)

View file

@ -4756,7 +4756,9 @@ func TestQuotaLimits(t *testing.T) {
err = sftpUploadFile(testFilePath1, testFileName1, testFileSize1, client) err = sftpUploadFile(testFilePath1, testFileName1, testFileSize1, client)
if assert.Error(t, err) { if assert.Error(t, err) {
assert.Contains(t, err.Error(), "SSH_FX_FAILURE") assert.Contains(t, err.Error(), "SSH_FX_FAILURE")
assert.Contains(t, err.Error(), common.ErrQuotaExceeded.Error()) if user.Username == localUser.Username {
assert.Contains(t, err.Error(), common.ErrQuotaExceeded.Error())
}
} }
_, err = client.Stat(testFileName1) _, err = client.Stat(testFileName1)
assert.Error(t, err) assert.Error(t, err)

View file

@ -17,7 +17,7 @@ package version
import "strings" import "strings"
const version = "2.5.6" const version = "2.5.7-dev"
var ( var (
commit = "" commit = ""