statusFromError: improve support for error wrapping

This commit is contained in:
Nicola Murino 2022-12-25 17:21:42 +01:00
parent 707787e8bc
commit 08880975fb
2 changed files with 11 additions and 6 deletions

View File

@ -615,13 +615,15 @@ func statusFromError(id uint32, err error) *sshFxpStatusPacket {
return ret
}
switch e := err.(type) {
case fxerr:
if errors.Is(err, io.EOF) {
ret.StatusError.Code = sshFxEOF
return ret
}
var e fxerr
if errors.As(err, &e) {
ret.StatusError.Code = uint32(e)
default:
if e == io.EOF {
ret.StatusError.Code = sshFxEOF
}
return ret
}
return ret

View File

@ -2,6 +2,7 @@ package sftp
import (
"errors"
"fmt"
"io"
"syscall"
"testing"
@ -19,6 +20,8 @@ func TestErrFxCode(t *testing.T) {
{err: syscall.ENOENT, fx: ErrSSHFxNoSuchFile},
{err: syscall.EPERM, fx: ErrSSHFxPermissionDenied},
{err: io.EOF, fx: ErrSSHFxEOF},
{err: fmt.Errorf("wrapped permission denied error: %w", ErrSSHFxPermissionDenied), fx: ErrSSHFxPermissionDenied},
{err: fmt.Errorf("wrapped op unsupported error: %w", ErrSSHFxOpUnsupported), fx: ErrSSHFxOpUnsupported},
}
for _, tt := range table {
statusErr := statusFromError(1, tt.err).StatusError