Skip to content

Commit 82820bb

Browse files
Remove special build tag to test errors 1.13 functions. Go 1.13 or higher is now required
1 parent f2e3117 commit 82820bb

File tree

2 files changed

+43
-66
lines changed

2 files changed

+43
-66
lines changed

errors_1_13_test.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

errors_test.go

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ package twirp
1515

1616
import (
1717
"encoding/json"
18+
"errors"
1819
"fmt"
1920
"net/http/httptest"
2021
"sync"
2122
"testing"
2223

23-
"github.com/pkg/errors"
24+
pkgerrors "github.com/pkg/errors"
2425
)
2526

2627
func TestWithMetaRaces(t *testing.T) {
@@ -43,15 +44,53 @@ func TestWithMetaRaces(t *testing.T) {
4344
}
4445
}
4546

46-
func TestErrorCause(t *testing.T) {
47-
rootCause := errors.New("this is only a test")
47+
func TestPkgErrorCause(t *testing.T) {
48+
rootCause := pkgerrors.New("this is only a test")
4849
twerr := InternalErrorWith(rootCause)
49-
cause := errors.Cause(twerr)
50+
cause := pkgerrors.Cause(twerr)
5051
if cause != rootCause {
5152
t.Errorf("got wrong cause for err. have=%q, want=%q", cause, rootCause)
5253
}
5354
}
5455

56+
func TestWrapError(t *testing.T) {
57+
rootCause := errors.New("cause")
58+
twerr := NewError(NotFound, "it ain't there")
59+
err := WrapError(twerr, rootCause)
60+
cause := pkgerrors.Cause(err)
61+
if cause != rootCause {
62+
t.Errorf("got wrong cause. got=%q, want=%q", cause, rootCause)
63+
}
64+
wantMsg := "twirp error not_found: it ain't there"
65+
if gotMsg := err.Error(); gotMsg != wantMsg {
66+
t.Errorf("got wrong error text. got=%q, want=%q", gotMsg, wantMsg)
67+
}
68+
}
69+
70+
type myError string
71+
72+
func (e myError) Error() string {
73+
return string(e)
74+
}
75+
76+
func TestInternalErrorWith_Unwrap(t *testing.T) {
77+
myErr := myError("myError")
78+
wrErr := fmt.Errorf("wrapped: %w", myErr) // double wrap
79+
twerr := InternalErrorWith(wrErr)
80+
81+
if !errors.Is(twerr, myErr) {
82+
t.Errorf("expected errors.Is to match the error wrapped by twirp.InternalErrorWith")
83+
}
84+
85+
var errTarget myError
86+
if !errors.As(twerr, &errTarget) {
87+
t.Errorf("expected errors.As to match the error wrapped by twirp.InternalErrorWith")
88+
}
89+
if errTarget.Error() != myErr.Error() {
90+
t.Errorf("invalid value for errTarget.Error(). have=%q, want=%q", errTarget.Error(), myErr.Error())
91+
}
92+
}
93+
5594
type errorResponeWriter struct {
5695
*httptest.ResponseRecorder
5796
}
@@ -132,17 +171,3 @@ func TestWriteError_WithNonTwirpError(t *testing.T) {
132171
return
133172
}
134173
}
135-
136-
func TestWrapError(t *testing.T) {
137-
rootCause := errors.New("cause")
138-
twerr := NewError(NotFound, "it ain't there")
139-
err := WrapError(twerr, rootCause)
140-
cause := errors.Cause(err)
141-
if cause != rootCause {
142-
t.Errorf("got wrong cause. got=%q, want=%q", cause, rootCause)
143-
}
144-
wantMsg := "twirp error not_found: it ain't there"
145-
if gotMsg := err.Error(); gotMsg != wantMsg {
146-
t.Errorf("got wrong error text. got=%q, want=%q", gotMsg, wantMsg)
147-
}
148-
}

0 commit comments

Comments
 (0)