Skip to content

Commit b5c21f3

Browse files
committed
Make special ExitError.Error for Go <1.14
1 parent 972c93b commit b5c21f3

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

tfexec/errors.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tfexec
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"os/exec"
87
"regexp"
@@ -216,29 +215,3 @@ type ExitError struct {
216215
func (e *ExitError) Unwrap() error {
217216
return e.err
218217
}
219-
220-
func (e *ExitError) Error() string {
221-
var out string
222-
ee, ok := e.err.(*exec.ExitError)
223-
if ok {
224-
out = fmt.Sprintf("%q (pid %d) exited (code %d): %s",
225-
e.args,
226-
ee.Pid(),
227-
ee.ExitCode(),
228-
ee.ProcessState.String())
229-
if e.ctxErr != nil {
230-
out += fmt.Sprintf("\n%s", e.ctxErr)
231-
}
232-
} else {
233-
out = fmt.Sprintf("%q exited: %s", e.args, e.err.Error())
234-
if e.ctxErr != nil && !errors.Is(e.err, e.ctxErr) {
235-
out += fmt.Sprintf("\n%s", e.ctxErr)
236-
}
237-
}
238-
239-
if e.stderr != "" {
240-
out += fmt.Sprintf("\nstderr: %q", e.stderr)
241-
}
242-
243-
return out
244-
}

tfexec/errors_exiterror.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// +build !go1.12,!go1.13
2+
3+
package tfexec
4+
5+
import (
6+
"errors"
7+
"fmt"
8+
"os/exec"
9+
)
10+
11+
func (e *ExitError) Error() string {
12+
var out string
13+
ee, ok := e.err.(*exec.ExitError)
14+
if ok {
15+
out = fmt.Sprintf("%q (pid %d) exited (code %d): %s",
16+
e.args,
17+
ee.Pid(),
18+
ee.ExitCode(),
19+
ee.ProcessState.String())
20+
if e.ctxErr != nil {
21+
out += fmt.Sprintf("\n%s", e.ctxErr)
22+
}
23+
} else {
24+
out = fmt.Sprintf("%q exited: %s", e.args, e.err.Error())
25+
if e.ctxErr != nil && !errors.Is(e.err, e.ctxErr) {
26+
out += fmt.Sprintf("\n%s", e.ctxErr)
27+
}
28+
}
29+
30+
if e.stderr != "" {
31+
out += fmt.Sprintf("\nstderr: %q", e.stderr)
32+
}
33+
34+
return out
35+
}

tfexec/errors_exiterror_go112.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// +build go1.12 go1.13
2+
3+
package tfexec
4+
5+
import (
6+
"fmt"
7+
"os/exec"
8+
)
9+
10+
func (e *ExitError) Error() string {
11+
var out string
12+
ee, ok := e.err.(*exec.ExitError)
13+
if ok {
14+
out = fmt.Sprintf("%q (pid %d) exited (code %d): %s",
15+
e.args,
16+
ee.Pid(),
17+
ee.ExitCode(),
18+
ee.ProcessState.String())
19+
if e.ctxErr != nil {
20+
out += fmt.Sprintf("\n%s", e.ctxErr)
21+
}
22+
} else {
23+
out = fmt.Sprintf("%q exited: %s", e.args, e.err.Error())
24+
if e.ctxErr != nil {
25+
out += fmt.Sprintf("\n%s", e.ctxErr)
26+
}
27+
}
28+
29+
if e.stderr != "" {
30+
out += fmt.Sprintf("\nstderr: %q", e.stderr)
31+
}
32+
33+
return out
34+
}

0 commit comments

Comments
 (0)