@@ -15,12 +15,13 @@ package twirp
15
15
16
16
import (
17
17
"encoding/json"
18
+ "errors"
18
19
"fmt"
19
20
"net/http/httptest"
20
21
"sync"
21
22
"testing"
22
23
23
- "github.com/pkg/errors"
24
+ pkgerrors "github.com/pkg/errors"
24
25
)
25
26
26
27
func TestWithMetaRaces (t * testing.T ) {
@@ -43,15 +44,53 @@ func TestWithMetaRaces(t *testing.T) {
43
44
}
44
45
}
45
46
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" )
48
49
twerr := InternalErrorWith (rootCause )
49
- cause := errors .Cause (twerr )
50
+ cause := pkgerrors .Cause (twerr )
50
51
if cause != rootCause {
51
52
t .Errorf ("got wrong cause for err. have=%q, want=%q" , cause , rootCause )
52
53
}
53
54
}
54
55
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
+
55
94
type errorResponeWriter struct {
56
95
* httptest.ResponseRecorder
57
96
}
@@ -132,17 +171,3 @@ func TestWriteError_WithNonTwirpError(t *testing.T) {
132
171
return
133
172
}
134
173
}
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