diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7967f6c..2eaad3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/_examples/openapi.gen.yaml b/_examples/openapi.gen.yaml index d789744..fc2760e 100644 --- a/_examples/openapi.gen.yaml +++ b/_examples/openapi.gen.yaml @@ -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' \ No newline at end of file + 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' \ No newline at end of file diff --git a/_examples/proto.ridl b/_examples/proto.ridl index 4ded560..6edb369 100644 --- a/_examples/proto.ridl +++ b/_examples/proto.ridl @@ -39,9 +39,11 @@ struct ComplexType - mapOfUsers: map - user: User - service ExampleService - GetUser(header: map, 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 \ No newline at end of file diff --git a/errorType.go.tmpl b/errorType.go.tmpl new file mode 100644 index 0000000..3718f04 --- /dev/null +++ b/errorType.go.tmpl @@ -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 -}} \ No newline at end of file diff --git a/main.go.tmpl b/main.go.tmpl index 649cc91..8ff6585 100644 --- a/main.go.tmpl +++ b/main.go.tmpl @@ -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 -}}