-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrapping errors instead of merely embedding error messages allows calling code to use `errors.Is` and `errors.As` to more precisely check the reasons for various errors instead of having to rely only on substring searches. We implement our own wrapper error to retain backward compatibility prior to Go 1.13, where there is no support for the "%w" format string in `fmt.Errorf`.
- Loading branch information
Showing
13 changed files
with
147 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// The concept of "wrapping" errors was only introduced in Go 1.13, so we only | ||
// want to test that our errors behave like wrapped errors on Go versions that | ||
// support `errors.Is`. | ||
//go:build go1.13 | ||
// +build go1.13 | ||
|
||
package mg | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
// TestErrorUnwrap checks that [errors.Is] can detect the underlying error in a | ||
// wrappedError. | ||
func TestErrorUnwrap(t *testing.T) { | ||
strError := errors.New("main error") | ||
underlyingError := errors.New("underlying error") | ||
actual := WrapError(underlyingError, strError) | ||
|
||
if !errors.Is(actual, underlyingError) { | ||
t.Fatalf("Expected outer error %#v to include %#v", actual, underlyingError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// The concept of "wrapping" errors was only introduced in Go 1.13, so we only | ||
// want to test that our errors behave like wrapped errors on Go versions that | ||
// support `errors.Is`. | ||
//go:build go1.13 | ||
// +build go1.13 | ||
|
||
package sh | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestWrappedError(t *testing.T) { | ||
_, err := Exec(nil, nil, nil, os.Args[0]+"-doesnotexist", "-printArgs", "foo") | ||
if err == nil { | ||
t.Fatalf("Expected to fail running %s", os.Args[0]+"-doesnotexist") | ||
} | ||
if !errors.Is(err, os.ErrNotExist) { | ||
t.Fatalf("Expected error to be like ErrNotExist, got %#v", err) | ||
} | ||
} |
Oops, something went wrong.