fix the error message for errors that occur during file transfers

we should special case path errors and replace the fs path with the
virtual path.

Thanks to @nezzzumi for reporting this issue

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino 2024-05-10 16:04:44 +02:00
parent a6a92f0d69
commit a73c6569f9
No known key found for this signature in database
GPG key ID: 935D2952DEC4EECF
4 changed files with 20 additions and 4 deletions

View file

@ -18,6 +18,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"strings"
@ -1665,9 +1666,10 @@ func (c *BaseConnection) GetGenericError(err error) error {
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, err.Error())
}
if err != nil {
if e, ok := err.(*os.PathError); ok {
c.Log(logger.LevelError, "generic path error: %+v", e)
return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, e.Op, e.Err.Error())
var pathError *fs.PathError
if errors.As(err, &pathError) {
c.Log(logger.LevelError, "generic path error: %+v", pathError)
return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, pathError.Op, pathError.Err.Error())
}
c.Log(logger.LevelError, "generic error: %+v", err)
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrGenericFailure.Error())

View file

@ -16,6 +16,8 @@ package common
import (
"errors"
"fmt"
"io/fs"
"path"
"sync"
"sync/atomic"
@ -220,6 +222,10 @@ func (t *BaseTransfer) ConvertError(err error) error {
} else if t.Fs.IsPermission(err) {
return t.Connection.GetPermissionDeniedError()
}
var pathError *fs.PathError
if errors.As(err, &pathError) {
return fmt.Errorf("%s %s: %s", pathError.Op, t.GetVirtualPath(), pathError.Err.Error())
}
return err
}

View file

@ -16,6 +16,7 @@ package common
import (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
@ -226,6 +227,13 @@ func TestTransferErrors(t *testing.T) {
conn := NewBaseConnection("id", ProtocolSFTP, "", "", u)
transfer := NewBaseTransfer(file, conn, nil, testFile, testFile, "/transfer_test_file", TransferUpload,
0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
pathError := &os.PathError{
Op: "test",
Path: testFile,
Err: os.ErrInvalid,
}
err = transfer.ConvertError(pathError)
assert.EqualError(t, err, fmt.Sprintf("%s %s: %s", pathError.Op, "/transfer_test_file", pathError.Err.Error()))
err = transfer.ConvertError(os.ErrNotExist)
assert.ErrorIs(t, err, sftp.ErrSSHFxNoSuchFile)
err = transfer.ConvertError(os.ErrPermission)

View file

@ -1722,7 +1722,7 @@ func TestSCPUploadFiledata(t *testing.T) {
transfer.Connection.AddTransfer(transfer)
err = scpCommand.getUploadFileData(2, transfer)
assert.True(t, errors.Is(err, os.ErrClosed))
assert.ErrorContains(t, err, os.ErrClosed.Error())
err = os.Remove(testfile)
assert.NoError(t, err)