-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror_checker.go
48 lines (39 loc) · 1.36 KB
/
error_checker.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
package cmdchain
import "os/exec"
// ErrorChecker is a function which will receive the command's error. His purposes is to check if the given error can
// be ignored. If the function return true the given error is a "real" error and will NOT be ignored!
type ErrorChecker func(index int, command *exec.Cmd, err error) bool
// IgnoreExitCode will return an ErrorChecker. This will ignore all exec.ExitError which have any of the given exit codes.
func IgnoreExitCode(allowedCodes ...int) ErrorChecker {
return func(_ int, _ *exec.Cmd, err error) bool {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode := exitErr.ExitCode()
for _, allowedCode := range allowedCodes {
if allowedCode == exitCode {
return false
}
}
}
// its a "true" error
return true
}
}
// IgnoreExitErrors will return an ErrorChecker. This will ignore all exec.ExitError.
func IgnoreExitErrors() ErrorChecker {
return func(_ int, _ *exec.Cmd, err error) bool {
_, isExitError := err.(*exec.ExitError)
return !isExitError
}
}
// IgnoreAll will return an ErrorChecker. This will ignore all error.
func IgnoreAll() ErrorChecker {
return func(_ int, _ *exec.Cmd, _ error) bool {
return false
}
}
// IgnoreNothing will return an ErrorChecker. This will ignore no error.
func IgnoreNothing() ErrorChecker {
return func(_ int, _ *exec.Cmd, _ error) bool {
return true
}
}