-
Notifications
You must be signed in to change notification settings - Fork 152
/
exit_errors.go
51 lines (40 loc) · 935 Bytes
/
exit_errors.go
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
47
48
49
50
51
package fluxexec
import (
"context"
"fmt"
"os/exec"
"strings"
)
func (flux *Flux) wrapExitError(ctx context.Context, err error, stderr string) error {
exitErr, ok := err.(*exec.ExitError)
if !ok {
// not an exit error, short circuit, nothing to wrap
return err
}
ctxErr := ctx.Err()
// nothing to parse, return early
errString := strings.TrimSpace(stderr)
if errString == "" {
return &unwrapper{exitErr, ctxErr}
}
// TODO parse stderr for error message here
return fmt.Errorf("%w\n%s", &unwrapper{exitErr, ctxErr}, stderr)
}
type unwrapper struct {
err error
ctxErr error
}
func (u *unwrapper) Unwrap() error {
return u.err
}
func (u *unwrapper) Is(target error) bool {
switch target {
case context.DeadlineExceeded, context.Canceled:
return u.ctxErr == context.DeadlineExceeded ||
u.ctxErr == context.Canceled
}
return false
}
func (u *unwrapper) Error() string {
return u.err.Error()
}