From e88e92c0ccca5e5a22febee39f846063125e92bb Mon Sep 17 00:00:00 2001 From: Brad Peabody Date: Thu, 26 Mar 2020 16:52:55 -0700 Subject: [PATCH] added Unwrap method to errWrapped plus tests; switched travis to go 1.14 --- .travis.yml | 2 +- msgp/errors.go | 3 +++ msgp/errors_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5f574c09..0a18a6d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - 1.12.x + - 1.14.x - tip env: diff --git a/msgp/errors.go b/msgp/errors.go index cc78a980..921e8553 100644 --- a/msgp/errors.go +++ b/msgp/errors.go @@ -123,6 +123,9 @@ func (e errWrapped) Resumable() bool { return resumableDefault } +// Unwrap returns the cause. +func (e errWrapped) Unwrap() error { return e.cause } + type errShort struct{} func (e errShort) Error() string { return "msgp: too few bytes left to read object" } diff --git a/msgp/errors_test.go b/msgp/errors_test.go index 308bec64..5c43427a 100644 --- a/msgp/errors_test.go +++ b/msgp/errors_test.go @@ -3,6 +3,7 @@ package msgp import ( "errors" "fmt" + "io" "strings" "testing" ) @@ -87,3 +88,44 @@ func TestCauseShortByte(t *testing.T) { t.Fatal() } } + +func TestUnwrap(t *testing.T) { + + // check errors that get wrapped + for idx, err := range []error{ + errors.New("test"), + io.EOF, + } { + t.Run(fmt.Sprintf("wrapped_%d", idx), func(t *testing.T) { + cerr := WrapError(err, "test") + if cerr == err { + t.Fatal() + } + uwerr := errors.Unwrap(cerr) + if uwerr != err { + t.Fatal() + } + if !errors.Is(cerr, err) { + t.Fatal() + } + }) + } + + // check errors where only context is applied + for idx, err := range []error{ + ArrayError{}, + &ErrUnsupportedType{}, + } { + t.Run(fmt.Sprintf("ctx_only_%d", idx), func(t *testing.T) { + cerr := WrapError(err, "test") + if cerr == err { + t.Fatal() + } + if errors.Unwrap(cerr) != nil { + t.Fatal() + } + + }) + } + +}