Skip to content

Commit 5de497d

Browse files
authored
Implement interoperability tests for schema errors (v0.11.0) (webrpc#193)
* Implement interoperability tests for schema errors * Update test RIDL to be backward compatible with legacy errors * Regenerate interoperability tests with github.com/webrpc/gen-golang@tags/errors * Make httpStatus a required field
1 parent 4babe29 commit 5de497d

9 files changed

Lines changed: 522 additions & 603 deletions

File tree

_examples/golang-basics/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ dev-generate:
88
../../bin/webrpc-gen -schema=example.ridl -target=golang -pkg=main -server -client -out=./example.gen.go
99

1010
dev-generate-local-templates:
11-
../../bin/webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -out=./example.gen.go
11+
../../bin/webrpc-gen -schema=example.ridl -target=github.com/webrpc/gen-golang@tags/errors -pkg=main -server -client -out=./example.gen.go

schema/error.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Error struct {
99
Code int `json:"code"`
1010
Name string `json:"name"`
1111
Message string `json:"message"`
12-
HTTPStatus int `json:"httpStatus,omitempty"`
12+
HTTPStatus int `json:"httpStatus"`
1313

1414
// Schema *WebRPCSchema `json:"-"` // denormalize/back-reference
1515
}
@@ -19,11 +19,8 @@ func (s *Error) Parse(schema *WebRPCSchema) error {
1919
if s.Name == "" {
2020
return fmt.Errorf("schema error: name cannot be empty")
2121
}
22-
if s.Code == 0 {
23-
return fmt.Errorf("schema error: error code cannot be 0")
24-
}
25-
if s.Code < 100 {
26-
return fmt.Errorf("schema error: error code must 100 or greater, '%s'", s.Name)
22+
if s.Code <= 0 {
23+
return fmt.Errorf("schema error: error code must be positive number")
2724
}
2825
n := strings.Fields(s.Name)
2926
if len(n) > 1 {

tests/_testdata/test.golden.json

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,116 @@
121121
]
122122
}
123123
],
124-
"errors": [],
124+
"errors": [
125+
{
126+
"code": 1,
127+
"name": "Unauthorized",
128+
"message": "unauthorized",
129+
"httpStatus": 401
130+
},
131+
{
132+
"code": 2,
133+
"name": "ExpiredToken",
134+
"message": "expired token",
135+
"httpStatus": 401
136+
},
137+
{
138+
"code": 3,
139+
"name": "InvalidToken",
140+
"message": "invalid token",
141+
"httpStatus": 401
142+
},
143+
{
144+
"code": 4,
145+
"name": "Deactivated",
146+
"message": "account deactivated",
147+
"httpStatus": 403
148+
},
149+
{
150+
"code": 5,
151+
"name": "ConfirmAccount",
152+
"message": "confirm your email",
153+
"httpStatus": 403
154+
},
155+
{
156+
"code": 6,
157+
"name": "AccessDenied",
158+
"message": "access denied",
159+
"httpStatus": 403
160+
},
161+
{
162+
"code": 7,
163+
"name": "MissingArgument",
164+
"message": "missing argument",
165+
"httpStatus": 400
166+
},
167+
{
168+
"code": 8,
169+
"name": "UnexpectedValue",
170+
"message": "unexpected value",
171+
"httpStatus": 400
172+
},
173+
{
174+
"code": 100,
175+
"name": "RateLimited",
176+
"message": "too many requests",
177+
"httpStatus": 429
178+
},
179+
{
180+
"code": 101,
181+
"name": "DatabaseDown",
182+
"message": "service outage",
183+
"httpStatus": 503
184+
},
185+
{
186+
"code": 102,
187+
"name": "ElasticDown",
188+
"message": "search is degraded",
189+
"httpStatus": 503
190+
},
191+
{
192+
"code": 103,
193+
"name": "NotImplemented",
194+
"message": "not implemented",
195+
"httpStatus": 501
196+
},
197+
{
198+
"code": 200,
199+
"name": "UserNotFound",
200+
"message": "user not found",
201+
"httpStatus": 400
202+
},
203+
{
204+
"code": 201,
205+
"name": "UserBusy",
206+
"message": "user busy",
207+
"httpStatus": 400
208+
},
209+
{
210+
"code": 202,
211+
"name": "InvalidUsername",
212+
"message": "invalid username",
213+
"httpStatus": 400
214+
},
215+
{
216+
"code": 300,
217+
"name": "FileTooBig",
218+
"message": "file is too big (max 1GB)",
219+
"httpStatus": 400
220+
},
221+
{
222+
"code": 301,
223+
"name": "FileInfected",
224+
"message": "file is infected",
225+
"httpStatus": 400
226+
},
227+
{
228+
"code": 302,
229+
"name": "FileType",
230+
"message": "unsupported file type",
231+
"httpStatus": 400
232+
}
233+
],
125234
"services": [
126235
{
127236
"name": "TestApi",
@@ -221,6 +330,17 @@
221330
}
222331
],
223332
"outputs": []
333+
},
334+
{
335+
"name": "GetSchemaError",
336+
"inputs": [
337+
{
338+
"name": "code",
339+
"type": "int",
340+
"optional": false
341+
}
342+
],
343+
"outputs": []
224344
}
225345
]
226346
}

0 commit comments

Comments
 (0)