Skip to content

Commit

Permalink
Merge pull request #535 from drakkan/errwrap
Browse files Browse the repository at this point in the history
statusFromError: improve support for error wrapping
  • Loading branch information
drakkan authored Jan 4, 2023
2 parents 707787e + 0888097 commit 2489717
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
14 changes: 8 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions sftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sftp

import (
"errors"
"fmt"
"io"
"syscall"
"testing"
Expand All @@ -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
Expand Down

0 comments on commit 2489717

Please sign in to comment.