-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy patherrors_test.go
More file actions
206 lines (183 loc) · 4.83 KB
/
Copy patherrors_test.go
File metadata and controls
206 lines (183 loc) · 4.83 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2026 The Go Language Server Authors. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"errors"
"fmt"
"testing"
gocmp "github.com/google/go-cmp/cmp"
)
func TestError_Error(t *testing.T) {
t.Parallel()
if got := NewError(InvalidParams, "bad").Error(); got != "bad" {
t.Errorf("Error() = %q, want %q", got, "bad")
}
var nilErr *Error
if got := nilErr.Error(); got != "" {
t.Errorf("nil Error() = %q, want empty", got)
}
}
func TestErrorf(t *testing.T) {
t.Parallel()
e := Errorf(InternalError, "failed %d times", 3)
if e.Code != InternalError {
t.Errorf("Code = %d, want %d", e.Code, InternalError)
}
if e.Message != "failed 3 times" {
t.Errorf("Message = %q, want %q", e.Message, "failed 3 times")
}
}
func TestError_Is(t *testing.T) {
t.Parallel()
tests := map[string]struct {
err error
target error
want bool
}{
"success: same code matches sentinel": {
err: NewError(ParseError, "custom message"),
target: ErrParse,
want: true,
},
"success: different code does not match": {
err: NewError(ParseError, "x"),
target: ErrInvalidRequest,
want: false,
},
"success: wrapped error matches via errors.Is": {
err: fmt.Errorf("context: %w", NewError(MethodNotFound, "x")),
target: ErrMethodNotFound,
want: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
if got := errors.Is(tt.err, tt.target); got != tt.want {
t.Errorf("errors.Is = %v, want %v", got, tt.want)
}
})
}
}
func TestToWireError(t *testing.T) {
t.Parallel()
tests := map[string]struct {
err error
wantCode Code
wantMsg string
wantNil bool
}{
"success: nil maps to nil": {
err: nil,
wantNil: true,
},
"success: wire error preserved verbatim": {
err: NewError(InvalidParams, "invalid params"),
wantCode: InvalidParams,
wantMsg: "invalid params",
},
"success: plain error maps to message with zero code": {
err: errors.New("boom"),
wantCode: 0,
wantMsg: "boom",
},
"success: wrapped wire error preserves code with outer message": {
err: fmt.Errorf("handler failed: %w", NewError(MethodNotFound, "method not found")),
wantCode: MethodNotFound,
wantMsg: "handler failed: method not found",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
got := toWireError(tt.err)
if tt.wantNil {
if got != nil {
t.Fatalf("toWireError = %v, want nil", got)
}
return
}
if got == nil {
t.Fatal("toWireError = nil, want non-nil")
}
if got.Code != tt.wantCode {
t.Errorf("Code = %d, want %d", got.Code, tt.wantCode)
}
if got.Message != tt.wantMsg {
t.Errorf("Message = %q, want %q", got.Message, tt.wantMsg)
}
})
}
}
func TestError_WireRoundTrip(t *testing.T) {
t.Parallel()
tests := map[string]struct {
err *Error
}{
"success: code and message": {
err: NewError(InvalidParams, "invalid params"),
},
"success: code message and data": {
err: &Error{Code: InternalError, Message: "boom", Data: RawMessage(`{"k":"v"}`)},
},
"success: message needing escape": {
err: NewError(ParseError, "line\t1 \"quoted\""),
},
"success: negative custom code": {
err: NewError(Code(-32050), "custom"),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
encoded := appendError(nil, tt.err)
got, ok := decodeError(encoded)
if !ok {
t.Fatalf("decodeError failed for %q", encoded)
}
if diff := gocmp.Diff(tt.err, got); diff != "" {
t.Errorf("error round-trip mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestError_CodePreservedThroughResponse(t *testing.T) {
t.Parallel()
// Code must survive encode -> decode of a full error response.
resp := NewResponse(NewNumberID(1), nil, NewError(MethodNotFound, "method not found"))
encoded, err := EncodeMessage(resp)
if err != nil {
t.Fatalf("EncodeMessage error: %v", err)
}
decoded, err := DecodeMessage(encoded)
if err != nil {
t.Fatalf("DecodeMessage error: %v", err)
}
got, ok := decoded.(*Response)
if !ok {
t.Fatalf("decoded type = %T, want *Response", decoded)
}
var werr *Error
if !errors.As(got.Err(), &werr) {
t.Fatalf("response error is not *Error: %v", got.Err())
}
if werr.Code != MethodNotFound {
t.Errorf("decoded code = %d, want %d", werr.Code, MethodNotFound)
}
}
func TestToWireError_PreservesData(t *testing.T) {
t.Parallel()
inner := &Error{
Code: InvalidParams,
Message: "invalid params",
Data: RawMessage(`{"field":"name"}`),
}
wrapped := fmt.Errorf("bad request: %w", inner)
got := toWireError(wrapped)
if got.Code != InvalidParams {
t.Errorf("got code %v, want %v", got.Code, InvalidParams)
}
if string(got.Data) != `{"field":"name"}` {
t.Errorf("got data %q, want %q", got.Data, `{"field":"name"}`)
}
}