Skip to content

Commit

Permalink
added Unwrap method to errWrapped plus tests; switched travis to go 1.14
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleypeabody authored and philhofer committed Mar 27, 2020
1 parent 4253cda commit e88e92c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- 1.12.x
- 1.14.x
- tip

env:
Expand Down
3 changes: 3 additions & 0 deletions msgp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
42 changes: 42 additions & 0 deletions msgp/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package msgp
import (
"errors"
"fmt"
"io"
"strings"
"testing"
)
Expand Down Expand Up @@ -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()
}

})
}

}

0 comments on commit e88e92c

Please sign in to comment.