diff options
author | Paulo Gomes <pjbgf@linux.com> | 2024-05-28 20:20:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-28 20:20:36 +0100 |
commit | c4390a64249356c285c069c6cc833e81539a55d8 (patch) | |
tree | 0289af95f3f97c1a807663b5e2de97bfc04a9066 /internal | |
parent | c7feada87363f96c224214ba309efd5cf5a886fc (diff) | |
parent | 4070322588104631421ebb03d80dc293d7d5a54f (diff) | |
download | go-git-c4390a64249356c285c069c6cc833e81539a55d8.tar.gz |
Merge pull request #1100 from ggambetti/master
plumbing: transport/http, Wrap http errors to return reason. Fixes #1097
Diffstat (limited to 'internal')
-rw-r--r-- | internal/test/checkers.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/test/checkers.go b/internal/test/checkers.go new file mode 100644 index 0000000..257d93d --- /dev/null +++ b/internal/test/checkers.go @@ -0,0 +1,43 @@ +package test + +import ( + "errors" + "fmt" + + check "gopkg.in/check.v1" +) + +// This check.Checker implementation exists because there's no implementation +// in the library that compares errors using `errors.Is`. If / when the check +// library fixes https://github.com/go-check/check/issues/139, this code can +// likely be removed and replaced with the library implementation. +// +// Added in Go 1.13 [https://go.dev/blog/go1.13-errors] `errors.Is` is the +// best mechanism to use to compare errors that might be wrapped in other +// errors. +type errorIsChecker struct { + *check.CheckerInfo +} + +var ErrorIs check.Checker = errorIsChecker{ + &check.CheckerInfo{ + Name: "ErrorIs", + Params: []string{"obtained", "expected"}, + }, +} + +func (e errorIsChecker) Check(params []interface{}, names []string) (bool, string) { + obtained, ok := params[0].(error) + if !ok { + return false, "obtained is not an error" + } + expected, ok := params[1].(error) + if !ok { + return false, "expected is not an error" + } + + if !errors.Is(obtained, expected) { + return false, fmt.Sprintf("obtained: %+v expected: %+v", obtained, expected) + } + return true, "" +} |