Skip to content

Commit 72c9b7b

Browse files
committed
Wrap sftp errors because SSH_FX_FAILURE is not that helpful
1 parent 8110a0d commit 72c9b7b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

provider/pkg/provider/remote/copyController.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func copy(ctx context.Context, input CopyInputs) (CopyOutputs, error) {
135135
func remoteStat(sftp *sftp.Client, path string) (fs.FileInfo, error) {
136136
info, err := sftp.Stat(path)
137137
if err != nil && !errors.Is(err, os.ErrNotExist) {
138-
return nil, err
138+
return nil, fmt.Errorf("failed to stat remote path %s: %w", path, err)
139139
}
140140
return info, nil
141141
}
@@ -171,15 +171,15 @@ func sftpCopy(sftp *sftp.Client, sourcePath, destPath string) error {
171171
if destStat == nil {
172172
err = sftp.Mkdir(dest)
173173
if err != nil {
174-
return err
174+
return fmt.Errorf("failed to create remote directory %s: %w", dest, err)
175175
}
176176
}
177177

178178
if !strings.HasSuffix(sourcePath, "/") {
179179
dest = filepath.Join(dest, filepath.Base(sourcePath))
180180
err = sftp.Mkdir(dest)
181181
if err != nil {
182-
return err
182+
return fmt.Errorf("failed to create remote directory %s: %w", dest, err)
183183
}
184184
}
185185
err = copyDir(sftp, sourcePath, dest)
@@ -202,12 +202,12 @@ func copyFile(sftp *sftp.Client, src, dst string) error {
202202

203203
remote, err := sftp.Create(dst)
204204
if err != nil {
205-
return err
205+
return fmt.Errorf("failed to create remote file %s: %w", dst, err)
206206
}
207207
defer remote.Close()
208208

209209
_, err = remote.ReadFrom(local)
210-
return err
210+
return fmt.Errorf("failed to copy file %s to remote path %s: %w", src, dst, err)
211211
}
212212

213213
// copyDir copies a directory recursively from the local file system to a remote host.
@@ -228,11 +228,13 @@ func copyDir(sftp *sftp.Client, src, dst string) error {
228228
dirInfo, err := sftp.Stat(remotePath)
229229
// sftp normalizes the error to os.ErrNotExist, see client.go: normaliseError.
230230
if err != nil && !errors.Is(err, os.ErrNotExist) {
231-
return err
231+
return fmt.Errorf("failed to stat remote path %s: %w", remotePath, err)
232232
}
233233

234234
if dirInfo == nil {
235-
return sftp.Mkdir(remotePath)
235+
if err = sftp.Mkdir(remotePath); err != nil {
236+
return fmt.Errorf("failed to create remote directory %s: %w", remotePath, err)
237+
}
236238
} else if !dirInfo.IsDir() {
237239
return fmt.Errorf("remote path %s exists but is not a directory", remotePath)
238240
}

0 commit comments

Comments
 (0)