Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
173 lines (164 loc) · 5.67 KB
/
Copy patherrors_test.go
File metadata and controls
173 lines (164 loc) · 5.67 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
package acp
import (
"context"
"encoding/json"
"errors"
"io"
"os"
"testing"
"time"
)
func TestToReqErr_ContextCanceledMapsToRequestCancelled(t *testing.T) {
wrapped := errors.Join(context.Canceled, errors.New("extra context"))
re := toReqErr(wrapped)
if re == nil {
t.Fatal("expected request error")
}
if re.Code != -32800 {
t.Fatalf("expected code -32800, got %d", re.Code)
}
}
func TestToReqErr_DeadlineExceededMapsToInternalError(t *testing.T) {
re := toReqErr(context.DeadlineExceeded)
if re == nil {
t.Fatal("expected request error")
}
if re.Code != -32603 {
t.Fatalf("expected code -32603, got %d", re.Code)
}
}
// The data of an error from the peer reaches the caller with every digit.
func TestRequestError_DataFromThePeerKeepsEveryDigit(t *testing.T) {
c2aR, c2aW := io.Pipe()
a2cR, a2cW := io.Pipe()
const data = `{"id":9007199254740993}`
NewAgentSideConnection(agentFuncs{
DeleteSessionFunc: func(context.Context, DeleteSessionRequest) (DeleteSessionResponse, error) {
return DeleteSessionResponse{}, NewRequestError(-32002, "Resource not found", json.RawMessage(data))
},
}, a2cW, c2aR)
c := NewClientSideConnection(&clientFuncs{}, c2aW, a2cR)
initializeClient(t, c)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := c.DeleteSession(ctx, DeleteSessionRequest{SessionId: "s"})
var re *RequestError
if !errors.As(err, &re) || re.Code != -32002 || string(re.Data) != data {
t.Fatalf("DeleteSession error = %v, want -32002 with data %s", err, data)
}
if got := re.Error(); got != `{"code":-32002,"message":"Resource not found","data":`+data+`}` {
t.Errorf("Error() = %s", got)
}
}
func TestNewRequestError_EncodesData(t *testing.T) {
tests := []struct {
data any
want string
}{
{map[string]string{"k": "v"}, `{"k":"v"}`},
{"text", `"text"`},
{nil, ""},
{func() {}, ""},
}
for _, tt := range tests {
if got := NewRequestError(1, "m", tt.data).Data; string(got) != tt.want {
t.Errorf("NewRequestError data %#v = %s, want %s", tt.data, got, tt.want)
}
}
if got := NewMethodNotFound("x/y").Error(); got != `{"code":-32601,"message":"Method not found","data":{"method":"x/y"}}` {
t.Errorf("NewMethodNotFound Error() = %s", got)
}
}
// A handler error that does not encode still answers the request, with an
// internal error, so that the caller does not wait until its ctx ends.
func TestRequestError_DataThatIsNotJSONAnswersWithAnInternalError(t *testing.T) {
c2aR, c2aW := io.Pipe()
a2cR, a2cW := io.Pipe()
NewAgentSideConnection(agentFuncs{
DeleteSessionFunc: func(context.Context, DeleteSessionRequest) (DeleteSessionResponse, error) {
return DeleteSessionResponse{}, &RequestError{Code: -32002, Message: "Resource not found", Data: json.RawMessage("not json")}
},
}, a2cW, c2aR)
c := NewClientSideConnection(&clientFuncs{}, c2aW, a2cR)
initializeClient(t, c)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := c.DeleteSession(ctx, DeleteSessionRequest{SessionId: "s"})
var re *RequestError
if !errors.As(err, &re) || re.Code != -32603 || ctx.Err() != nil {
t.Fatalf("DeleteSession error = %v (ctx %v), want a -32603 response for a handler error that does not encode", err, ctx.Err())
}
}
// RequestError keeps "data": null; the generated Error, whose data has
// x-deserialize-default-on-error, reads it as no data, as the Rust SDK does.
func TestRequestError_KeepsNullDataThatErrorDrops(t *testing.T) {
const in = `{"code":1,"message":"m","data":null}`
var re RequestError
if err := json.Unmarshal([]byte(in), &re); err != nil || string(re.Data) != "null" {
t.Fatalf("RequestError data = %q (error %v), want null", re.Data, err)
}
var e Error
if err := json.Unmarshal([]byte(in), &e); err != nil || e.Data != nil {
t.Fatalf("Error data = %q (error %v), want none", e.Data, err)
}
}
// TestNewConstructors_MatchTheSchemaErrorCodes checks each New* constructor
// against the code and title of its entry in the schema's ErrorCode.
func TestNewConstructors_MatchTheSchemaErrorCodes(t *testing.T) {
raw, err := os.ReadFile("schema/schema.json")
if err != nil {
t.Fatal(err)
}
var schema struct {
Defs struct {
ErrorCode struct {
AnyOf []struct {
Const *int `json:"const"`
Title string `json:"title"`
} `json:"anyOf"`
} `json:"ErrorCode"`
} `json:"$defs"`
}
if err := json.Unmarshal(raw, &schema); err != nil {
t.Fatal(err)
}
titles := map[int]string{}
for _, alt := range schema.Defs.ErrorCode.AnyOf {
if alt.Const != nil {
titles[*alt.Const] = alt.Title
}
}
constructors := map[string]*RequestError{
"NewParseError": NewParseError(nil),
"NewInvalidRequest": NewInvalidRequest(nil),
"NewMethodNotFound": NewMethodNotFound("m"),
"NewInvalidParams": NewInvalidParams(nil),
"NewInternalError": NewInternalError(nil),
"NewRequestCancelled": NewRequestCancelled(nil),
"NewAuthRequired": NewAuthRequired(nil),
"NewResourceNotFound": NewResourceNotFound(nil),
}
covered := map[int]bool{}
for name, e := range constructors {
title, ok := titles[e.Code]
if !ok {
t.Errorf("%s: code %d is not in the schema's ErrorCode", name, e.Code)
continue
}
if e.Message != title {
t.Errorf("%s: message %q, schema title %q", name, e.Message, title)
}
covered[e.Code] = true
}
for code, title := range titles {
if !covered[code] {
t.Errorf("no New* constructor for schema code %d (%s)", code, title)
}
}
}
func TestNewResourceNotFound_CarriesTheSessionID(t *testing.T) {
got := NewResourceNotFound(map[string]any{"sessionId": "s1"}).Error()
if want := `{"code":-32002,"message":"Resource not found","data":{"sessionId":"s1"}}`; got != want {
t.Fatalf("got %s, want %s", got, want)
}
}