-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherror.go
More file actions
46 lines (38 loc) · 1.02 KB
/
Copy patherror.go
File metadata and controls
46 lines (38 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package run
import (
"bytes"
"errors"
"fmt"
"io"
"os/exec"
)
// runError wraps exec.ExitError such that it always includes the embedded stderr.
type runError struct{ execErr *exec.ExitError }
var _ ExitCoder = &runError{}
// newError creats a new *Error, and can be provided a nil error and/or nil stdErr
func newError(err error, stdErr io.Reader) error {
if err == nil {
return nil
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
if stdErr != nil {
// Not assigned by default using cmd.Start(), so we consume our copy of stderr
// and set it here. If an error occurs we just don't do anything with stderr.
if b, err := io.ReadAll(stdErr); err == nil {
exitErr.Stderr = bytes.TrimSpace(b)
}
}
return &runError{execErr: exitErr}
}
return err
}
func (e *runError) Error() string {
if len(e.execErr.Stderr) == 0 {
return e.execErr.String()
}
return fmt.Sprintf("%s: %s", e.execErr.String(), string(e.execErr.Stderr))
}
func (e *runError) ExitCode() int {
return e.execErr.ExitCode()
}