forked from twitchtv/twirp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
173 lines (145 loc) · 4.48 KB
/
errors_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2018 Twitch Interactive, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the License is
// located at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// or in the "license" file accompanying this file. This file is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package twirp
import (
"encoding/json"
"errors"
"fmt"
"net/http/httptest"
"sync"
"testing"
pkgerrors "github.com/pkg/errors"
)
func TestWithMetaRaces(t *testing.T) {
err := NewError(Internal, "msg")
err = err.WithMeta("k1", "v1")
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(i int) {
_ = err.WithMeta(fmt.Sprintf("k-%d", i), "v")
wg.Done()
}(i)
}
wg.Wait()
if len(err.MetaMap()) != 1 {
t.Errorf("err was mutated")
}
}
func TestPkgErrorCause(t *testing.T) {
rootCause := pkgerrors.New("this is only a test")
twerr := InternalErrorWith(rootCause)
cause := pkgerrors.Cause(twerr)
if cause != rootCause {
t.Errorf("got wrong cause for err. have=%q, want=%q", cause, rootCause)
}
}
func TestWrapError(t *testing.T) {
rootCause := errors.New("cause")
twerr := NewError(NotFound, "it ain't there")
err := WrapError(twerr, rootCause)
cause := pkgerrors.Cause(err)
if cause != rootCause {
t.Errorf("got wrong cause. got=%q, want=%q", cause, rootCause)
}
wantMsg := "twirp error not_found: it ain't there"
if gotMsg := err.Error(); gotMsg != wantMsg {
t.Errorf("got wrong error text. got=%q, want=%q", gotMsg, wantMsg)
}
}
type myError string
func (e myError) Error() string {
return string(e)
}
func TestInternalErrorWith_Unwrap(t *testing.T) {
myErr := myError("myError")
wrErr := fmt.Errorf("wrapped: %w", myErr) // double wrap
twerr := InternalErrorWith(wrErr)
if !errors.Is(twerr, myErr) {
t.Errorf("expected errors.Is to match the error wrapped by twirp.InternalErrorWith")
}
var errTarget myError
if !errors.As(twerr, &errTarget) {
t.Errorf("expected errors.As to match the error wrapped by twirp.InternalErrorWith")
}
if errTarget.Error() != myErr.Error() {
t.Errorf("invalid value for errTarget.Error(). have=%q, want=%q", errTarget.Error(), myErr.Error())
}
}
type errorResponeWriter struct {
*httptest.ResponseRecorder
}
func (errorResponeWriter) Write([]byte) (int, error) {
return 0, errors.New("this is only a test")
}
func TestWriteError(t *testing.T) {
resp := httptest.NewRecorder()
twerr := NewError(Internal, "test middleware error")
err := WriteError(resp, twerr)
if err != nil {
t.Errorf("got an error from WriteError when not expecting one: %s", err)
return
}
twerrCode := ServerHTTPStatusFromErrorCode(twerr.Code())
if resp.Code != twerrCode {
t.Errorf("got wrong status. have=%d, want=%d", resp.Code, twerrCode)
return
}
var gotTwerrJSON twerrJSON
err = json.NewDecoder(resp.Body).Decode(&gotTwerrJSON)
if err != nil {
t.Errorf("got an error decoding response body: %s", err)
return
}
if ErrorCode(gotTwerrJSON.Code) != twerr.Code() {
t.Errorf("got wrong error code. have=%s, want=%s", gotTwerrJSON.Code, twerr.Code())
return
}
if gotTwerrJSON.Msg != twerr.Msg() {
t.Errorf("got wrong error message. have=%s, want=%s", gotTwerrJSON.Msg, twerr.Msg())
return
}
errResp := &errorResponeWriter{ResponseRecorder: resp}
// Writing again should error out as headers are being rewritten
err = WriteError(errResp, twerr)
if err == nil {
t.Errorf("did not get error on write. have=nil, want=some error")
}
}
func TestWriteError_WithNonTwirpError(t *testing.T) {
resp := httptest.NewRecorder()
nonTwerr := errors.New("not a twirp error")
err := WriteError(resp, nonTwerr)
if err != nil {
t.Errorf("got an error from WriteError when not expecting one: %s", err)
return
}
if resp.Code != 500 {
t.Errorf("got wrong status. have=%d, want=%d", resp.Code, 500)
return
}
var gotTwerrJSON twerrJSON
err = json.NewDecoder(resp.Body).Decode(&gotTwerrJSON)
if err != nil {
t.Errorf("got an error decoding response body: %s", err)
return
}
if ErrorCode(gotTwerrJSON.Code) != Internal {
t.Errorf("got wrong error code. have=%s, want=%s", gotTwerrJSON.Code, Internal)
return
}
if gotTwerrJSON.Msg != ""+nonTwerr.Error() {
t.Errorf("got wrong error message. have=%s, want=%s", gotTwerrJSON.Msg, nonTwerr.Error())
return
}
}