Skip to content

Commit

Permalink
Implement interoperability tests for schema errors (v0.11.0) (webrpc#193
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
VojtechVitek committed Mar 9, 2023
1 parent 4babe29 commit 5de497d
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 603 deletions.
2 changes: 1 addition & 1 deletion _examples/golang-basics/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dev-generate:
../../bin/webrpc-gen -schema=example.ridl -target=golang -pkg=main -server -client -out=./example.gen.go

dev-generate-local-templates:
../../bin/webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -out=./example.gen.go
../../bin/webrpc-gen -schema=example.ridl -target=github.com/webrpc/gen-golang@tags/errors -pkg=main -server -client -out=./example.gen.go
9 changes: 3 additions & 6 deletions schema/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Error struct {
Code int `json:"code"`
Name string `json:"name"`
Message string `json:"message"`
HTTPStatus int `json:"httpStatus,omitempty"`
HTTPStatus int `json:"httpStatus"`

// Schema *WebRPCSchema `json:"-"` // denormalize/back-reference
}
Expand All @@ -19,11 +19,8 @@ func (s *Error) Parse(schema *WebRPCSchema) error {
if s.Name == "" {
return fmt.Errorf("schema error: name cannot be empty")
}
if s.Code == 0 {
return fmt.Errorf("schema error: error code cannot be 0")
}
if s.Code < 100 {
return fmt.Errorf("schema error: error code must 100 or greater, '%s'", s.Name)
if s.Code <= 0 {
return fmt.Errorf("schema error: error code must be positive number")
}
n := strings.Fields(s.Name)
if len(n) > 1 {
Expand Down
122 changes: 121 additions & 1 deletion tests/_testdata/test.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,116 @@
]
}
],
"errors": [],
"errors": [
{
"code": 1,
"name": "Unauthorized",
"message": "unauthorized",
"httpStatus": 401
},
{
"code": 2,
"name": "ExpiredToken",
"message": "expired token",
"httpStatus": 401
},
{
"code": 3,
"name": "InvalidToken",
"message": "invalid token",
"httpStatus": 401
},
{
"code": 4,
"name": "Deactivated",
"message": "account deactivated",
"httpStatus": 403
},
{
"code": 5,
"name": "ConfirmAccount",
"message": "confirm your email",
"httpStatus": 403
},
{
"code": 6,
"name": "AccessDenied",
"message": "access denied",
"httpStatus": 403
},
{
"code": 7,
"name": "MissingArgument",
"message": "missing argument",
"httpStatus": 400
},
{
"code": 8,
"name": "UnexpectedValue",
"message": "unexpected value",
"httpStatus": 400
},
{
"code": 100,
"name": "RateLimited",
"message": "too many requests",
"httpStatus": 429
},
{
"code": 101,
"name": "DatabaseDown",
"message": "service outage",
"httpStatus": 503
},
{
"code": 102,
"name": "ElasticDown",
"message": "search is degraded",
"httpStatus": 503
},
{
"code": 103,
"name": "NotImplemented",
"message": "not implemented",
"httpStatus": 501
},
{
"code": 200,
"name": "UserNotFound",
"message": "user not found",
"httpStatus": 400
},
{
"code": 201,
"name": "UserBusy",
"message": "user busy",
"httpStatus": 400
},
{
"code": 202,
"name": "InvalidUsername",
"message": "invalid username",
"httpStatus": 400
},
{
"code": 300,
"name": "FileTooBig",
"message": "file is too big (max 1GB)",
"httpStatus": 400
},
{
"code": 301,
"name": "FileInfected",
"message": "file is infected",
"httpStatus": 400
},
{
"code": 302,
"name": "FileType",
"message": "unsupported file type",
"httpStatus": 400
}
],
"services": [
{
"name": "TestApi",
Expand Down Expand Up @@ -221,6 +330,17 @@
}
],
"outputs": []
},
{
"name": "GetSchemaError",
"inputs": [
{
"name": "code",
"type": "int",
"optional": false
}
],
"outputs": []
}
]
}
Expand Down
Loading

0 comments on commit 5de497d

Please sign in to comment.