mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-06 14:20:55 +03:00
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:
@@ -18,6 +18,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -1665,9 +1666,10 @@ func (c *BaseConnection) GetGenericError(err error) error {
|
|||||||
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, err.Error())
|
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, err.Error())
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if e, ok := err.(*os.PathError); ok {
|
var pathError *fs.PathError
|
||||||
c.Log(logger.LevelError, "generic path error: %+v", e)
|
if errors.As(err, &pathError) {
|
||||||
return fmt.Errorf("%w: %v %v", sftp.ErrSSHFxFailure, e.Op, e.Err.Error())
|
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)
|
c.Log(logger.LevelError, "generic error: %+v", err)
|
||||||
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrGenericFailure.Error())
|
return fmt.Errorf("%w: %v", sftp.ErrSSHFxFailure, ErrGenericFailure.Error())
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"path"
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -220,6 +222,10 @@ func (t *BaseTransfer) ConvertError(err error) error {
|
|||||||
} else if t.Fs.IsPermission(err) {
|
} else if t.Fs.IsPermission(err) {
|
||||||
return t.Connection.GetPermissionDeniedError()
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ package common
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -226,6 +227,13 @@ func TestTransferErrors(t *testing.T) {
|
|||||||
conn := NewBaseConnection("id", ProtocolSFTP, "", "", u)
|
conn := NewBaseConnection("id", ProtocolSFTP, "", "", u)
|
||||||
transfer := NewBaseTransfer(file, conn, nil, testFile, testFile, "/transfer_test_file", TransferUpload,
|
transfer := NewBaseTransfer(file, conn, nil, testFile, testFile, "/transfer_test_file", TransferUpload,
|
||||||
0, 0, 0, 0, true, fs, dataprovider.TransferQuota{})
|
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)
|
err = transfer.ConvertError(os.ErrNotExist)
|
||||||
assert.ErrorIs(t, err, sftp.ErrSSHFxNoSuchFile)
|
assert.ErrorIs(t, err, sftp.ErrSSHFxNoSuchFile)
|
||||||
err = transfer.ConvertError(os.ErrPermission)
|
err = transfer.ConvertError(os.ErrPermission)
|
||||||
|
|||||||
@@ -1722,7 +1722,7 @@ func TestSCPUploadFiledata(t *testing.T) {
|
|||||||
|
|
||||||
transfer.Connection.AddTransfer(transfer)
|
transfer.Connection.AddTransfer(transfer)
|
||||||
err = scpCommand.getUploadFileData(2, 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)
|
err = os.Remove(testfile)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user