Skip to content

Commit

Permalink
Implement support for webrpc errors (#5)
Browse files Browse the repository at this point in the history
* Add webrpc error schema

* Implement support for webrpc errors

* CI: Update to webrpc@v0.11.2

* Regenerate using webrpc@v0.11.2
VojtechVitek authored Jul 10, 2023
1 parent 4c31643 commit f4c9eac
Showing 5 changed files with 287 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ jobs:

- name: Install webrpc-gen
run: |
curl -o ./webrpc-gen -fLJO https://github.com/webrpc/webrpc/releases/download/v0.10.0/webrpc-gen.linux-amd64
curl -o ./webrpc-gen -fLJO https://github.com/webrpc/webrpc/releases/download/v0.11.2/webrpc-gen.linux-amd64
chmod +x ./webrpc-gen
echo $PWD >> $GITHUB_PATH
252 changes: 240 additions & 12 deletions _examples/openapi.gen.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# example v0.0.1 129e62d2f26f6a2e91ef2692ee971c7c0ef3b9bd
# example v0.0.1 8c962b82639c77d8c62f134f5c009611f35c7f8f
# --
# Code generated by webrpc-gen@v0.10.0 with custom generator; DO NOT EDIT
# Code generated by webrpc-gen@v0.11.2 with custom generator; DO NOT EDIT
#
# webrpc-gen -schema=./proto.ridl -target=../ -out=./openapi.gen.yaml -title=Example webrpc API -apiVersion=v22.11.8 -serverUrl=https://api.example.com -serverDescription=Production
openapi: 3.0.0
@@ -12,12 +12,204 @@ servers:
description: 'Production'
components:
schemas:
empty:
ErrorWebrpcEndpoint:
type: object
error:
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "WebrpcEndpoint"
code:
type: number
example: 0
msg:
type: string
example: "endpoint error"
cause:
type: string
status:
type: number
example: 400
ErrorWebrpcRequestFailed:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "WebrpcRequestFailed"
code:
type: number
example: -1
msg:
type: string
example: "request failed"
cause:
type: string
status:
type: number
example: 0
ErrorWebrpcBadRoute:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "WebrpcBadRoute"
code:
type: number
example: -2
msg:
type: string
example: "bad route"
cause:
type: string
status:
type: number
example: 404
ErrorWebrpcBadMethod:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "WebrpcBadMethod"
code:
type: number
example: -3
msg:
type: string
example: "bad method"
cause:
type: string
status:
type: number
example: 405
ErrorWebrpcBadRequest:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "WebrpcBadRequest"
code:
type: number
example: -4
msg:
type: string
example: "bad request"
cause:
type: string
status:
type: number
example: 400
ErrorWebrpcBadResponse:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "WebrpcBadResponse"
code:
type: number
example: -5
msg:
type: string
example: "bad response"
cause:
type: string
status:
type: number
example: 500
ErrorWebrpcServerPanic:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "WebrpcServerPanic"
code:
type: number
example: -6
msg:
type: string
example: "server panic"
cause:
type: string
status:
type: number
example: 500
ErrorRateLimited:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "RateLimited"
code:
type: number
example: 100
msg:
type: string
example: "too many requests"
cause:
type: string
status:
type: number
example: 429
ErrorDatabaseDown:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: "DatabaseDown"
code:
type: number
example: 101
msg:
type: string
example: "service outage"
cause:
type: string
status:
type: number
example: 503
Kind:
type: string
description: Represented as uint32 on the server side
@@ -173,12 +365,21 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/GetUser_Response'
default:
'4xx/5xx':
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
oneOf:
- $ref: '#/components/schemas/ErrorWebrpcEndpoint'
- $ref: '#/components/schemas/ErrorWebrpcRequestFailed'
- $ref: '#/components/schemas/ErrorWebrpcBadRoute'
- $ref: '#/components/schemas/ErrorWebrpcBadMethod'
- $ref: '#/components/schemas/ErrorWebrpcBadRequest'
- $ref: '#/components/schemas/ErrorWebrpcBadResponse'
- $ref: '#/components/schemas/ErrorWebrpcServerPanic'
- $ref: '#/components/schemas/ErrorRateLimited'
- $ref: '#/components/schemas/ErrorDatabaseDown'
/rpc/ExampleService/FindUser:
post:
requestBody:
@@ -193,12 +394,21 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/FindUser_Response'
default:
'4xx/5xx':
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
oneOf:
- $ref: '#/components/schemas/ErrorWebrpcEndpoint'
- $ref: '#/components/schemas/ErrorWebrpcRequestFailed'
- $ref: '#/components/schemas/ErrorWebrpcBadRoute'
- $ref: '#/components/schemas/ErrorWebrpcBadMethod'
- $ref: '#/components/schemas/ErrorWebrpcBadRequest'
- $ref: '#/components/schemas/ErrorWebrpcBadResponse'
- $ref: '#/components/schemas/ErrorWebrpcServerPanic'
- $ref: '#/components/schemas/ErrorRateLimited'
- $ref: '#/components/schemas/ErrorDatabaseDown'
/rpc/ExampleService/ListUsers:
post:
requestBody:
@@ -213,12 +423,21 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ListUsers_Response'
default:
'4xx/5xx':
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
oneOf:
- $ref: '#/components/schemas/ErrorWebrpcEndpoint'
- $ref: '#/components/schemas/ErrorWebrpcRequestFailed'
- $ref: '#/components/schemas/ErrorWebrpcBadRoute'
- $ref: '#/components/schemas/ErrorWebrpcBadMethod'
- $ref: '#/components/schemas/ErrorWebrpcBadRequest'
- $ref: '#/components/schemas/ErrorWebrpcBadResponse'
- $ref: '#/components/schemas/ErrorWebrpcServerPanic'
- $ref: '#/components/schemas/ErrorRateLimited'
- $ref: '#/components/schemas/ErrorDatabaseDown'
/rpc/ExampleService/GetComplex:
post:
requestBody:
@@ -233,9 +452,18 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/GetComplex_Response'
default:
'4xx/5xx':
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
oneOf:
- $ref: '#/components/schemas/ErrorWebrpcEndpoint'
- $ref: '#/components/schemas/ErrorWebrpcRequestFailed'
- $ref: '#/components/schemas/ErrorWebrpcBadRoute'
- $ref: '#/components/schemas/ErrorWebrpcBadMethod'
- $ref: '#/components/schemas/ErrorWebrpcBadRequest'
- $ref: '#/components/schemas/ErrorWebrpcBadResponse'
- $ref: '#/components/schemas/ErrorWebrpcServerPanic'
- $ref: '#/components/schemas/ErrorRateLimited'
- $ref: '#/components/schemas/ErrorDatabaseDown'
4 changes: 3 additions & 1 deletion _examples/proto.ridl
Original file line number Diff line number Diff line change
@@ -39,9 +39,11 @@ struct ComplexType
- mapOfUsers: map<string,User>
- user: User


service ExampleService
- GetUser(header: map<string,string>, userID: uint64) => (code: uint32, user: User)
- FindUser(s: SearchFilter) => (name: string, user: User)
- ListUsers() => (users: []User)
- GetComplex() => (complex: ComplexType)

error 100 RateLimited "too many requests" HTTP 429
error 101 DatabaseDown "service outage" HTTP 503
27 changes: 27 additions & 0 deletions errorType.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{- define "errorType" -}}
{{- $error := . -}}

Error{{$error.Name}}:
type: object
required:
- error
- code
- msg
- status
properties:
error:
type: string
example: {{printf "%q" $error.Name}}
code:
type: number
example: {{$error.Code}}
msg:
type: string
example: {{printf "%q" $error.Message}}
cause:
type: string
status:
type: number
example: {{$error.HTTPStatus}}

{{- end -}}
24 changes: 16 additions & 8 deletions main.go.tmpl
Original file line number Diff line number Diff line change
@@ -57,6 +57,8 @@

{{- $types := .Types -}}
{{- $services := .Services -}}
{{- $webrpcErrors := .WebrpcErrors -}}
{{- $schemaErrors := .Errors -}}

# {{.SchemaName}} {{.SchemaVersion}} {{.SchemaHash}}
# --
@@ -74,12 +76,12 @@ servers:
{{ end -}}
components:
schemas:
empty:
type: object
error:
properties:
error:
type: string
{{- range $_, $error := $webrpcErrors }}
{{ template "errorType" $error }}
{{- end -}}
{{- range $_, $error := $schemaErrors }}
{{ template "errorType" $error }}
{{- end -}}
{{- range $_, $type := $types }}
{{template "type" dict "Type" $type "TypeMap" $typeMap }}
{{- end -}}
@@ -121,12 +123,18 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/{{$method.Name}}_Response'
default:
'4xx/5xx':
description: Error
content:
application/json:
schema:
$ref: '#/components/schemas/error'
oneOf:
{{- range $_, $error := $webrpcErrors }}
- $ref: '#/components/schemas/Error{{$error.Name}}'
{{- end -}}
{{- range $_, $error := $schemaErrors }}
- $ref: '#/components/schemas/Error{{$error.Name}}'
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}

0 comments on commit f4c9eac

Please sign in to comment.