Skip to content

Commit

Permalink
Use fmt.Errorf to be able to unwrap error
Browse files Browse the repository at this point in the history
  • Loading branch information
johejo committed Dec 1, 2020
1 parent 2c8720d commit a6d5f0b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions shellwords_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package shellwords

import (
"errors"
"go/build"
"os"
"os/exec"
"path"
"reflect"
"testing"
Expand Down Expand Up @@ -183,9 +185,9 @@ func TestBacktickError(t *testing.T) {
if err == nil {
t.Fatal("Should be an error")
}
expected := "exit status 2:go Version: unknown command\nRun 'go help' for usage.\n"
if expected != err.Error() {
t.Fatalf("Expected %q, but %q", expected, err.Error())
var eerr *exec.ExitError
if !errors.As(err, &eerr) {
t.Fatal("Should be able to unwrap to *exec.ExitError")
}
_, err = parser.Parse(`echo $(echo1)`)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions util_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package shellwords

import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
Expand All @@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", errors.New(err.Error() + ":" + string(b))
return "", fmt.Errorf("%s: %w", string(b), err)
}
return strings.TrimSpace(string(b)), nil
}
4 changes: 2 additions & 2 deletions util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package shellwords

import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
Expand All @@ -23,7 +23,7 @@ func shellRun(line, dir string) (string, error) {
if eerr, ok := err.(*exec.ExitError); ok {
b = eerr.Stderr
}
return "", errors.New(err.Error() + ":" + string(b))
return "", fmt.Errorf("%s: %w", string(b), err)
}
return strings.TrimSpace(string(b)), nil
}

0 comments on commit a6d5f0b

Please sign in to comment.