Skip to content

Commit a6d5f0b

Browse files
committed
Use fmt.Errorf to be able to unwrap error
1 parent 2c8720d commit a6d5f0b

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

shellwords_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package shellwords
22

33
import (
4+
"errors"
45
"go/build"
56
"os"
7+
"os/exec"
68
"path"
79
"reflect"
810
"testing"
@@ -183,9 +185,9 @@ func TestBacktickError(t *testing.T) {
183185
if err == nil {
184186
t.Fatal("Should be an error")
185187
}
186-
expected := "exit status 2:go Version: unknown command\nRun 'go help' for usage.\n"
187-
if expected != err.Error() {
188-
t.Fatalf("Expected %q, but %q", expected, err.Error())
188+
var eerr *exec.ExitError
189+
if !errors.As(err, &eerr) {
190+
t.Fatal("Should be able to unwrap to *exec.ExitError")
189191
}
190192
_, err = parser.Parse(`echo $(echo1)`)
191193
if err == nil {

util_posix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package shellwords
44

55
import (
6-
"errors"
6+
"fmt"
77
"os"
88
"os/exec"
99
"strings"
@@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
2323
if eerr, ok := err.(*exec.ExitError); ok {
2424
b = eerr.Stderr
2525
}
26-
return "", errors.New(err.Error() + ":" + string(b))
26+
return "", fmt.Errorf("%s: %w", string(b), err)
2727
}
2828
return strings.TrimSpace(string(b)), nil
2929
}

util_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
package shellwords
44

55
import (
6-
"errors"
6+
"fmt"
77
"os"
88
"os/exec"
99
"strings"
@@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
2323
if eerr, ok := err.(*exec.ExitError); ok {
2424
b = eerr.Stderr
2525
}
26-
return "", errors.New(err.Error() + ":" + string(b))
26+
return "", fmt.Errorf("%s: %w", string(b), err)
2727
}
2828
return strings.TrimSpace(string(b)), nil
2929
}

0 commit comments

Comments
 (0)