Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed May 14, 2024
1 parent fce2616 commit 25e0c4d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 15 additions & 0 deletions zfs/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package zfs

import (
"fmt"
)

type CommandError struct {
Args []string
Cause error
Stderr string
}

func (m *CommandError) Error() string {
return fmt.Sprintf("%s failed with %v: %s", m.Args, m.Cause, m.Stderr)
}
10 changes: 9 additions & 1 deletion zfs/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ func (t *Transfer) run() error {

if err == nil {
// It is the first failed process
err = fmt.Errorf("%s failed with %v: %s", cmd.Args, e, cmd.Stderr.(*bytes.Buffer).String())
err = &CommandError{
Args: cmd.Args,
Cause: e,
Stderr: cmd.Stderr.(*bytes.Buffer).String(),
}
}
}

Expand All @@ -128,11 +132,14 @@ func (t *Transfer) run() error {
}

// runs the recv command
recvWg := sync.WaitGroup{}
recvWg.Add(1)
recv := func() {
if e := recvCommand.Run(); e != nil {
setErr(e, recvCommand)
out.Close()
}
recvWg.Done()
}

/*
Expand All @@ -148,6 +155,7 @@ func (t *Transfer) run() error {
go recv()
copy()
e = sendCommand.Wait()
recvWg.Wait()
}

if e != nil {
Expand Down

0 comments on commit 25e0c4d

Please sign in to comment.