Skip to content

Commit

Permalink
allow for empty body in the exception (#690)
Browse files Browse the repository at this point in the history
* allow for empty body in the exception

thrift doesn't allow for a void in the exception hence using annontation
to indicate that the exception doesn't have any body.

* add new line in some exception handling cases for proper formating
  • Loading branch information
rpatali committed Mar 16, 2020
1 parent 6deb009 commit 993a605
Show file tree
Hide file tree
Showing 29 changed files with 1,102 additions and 56 deletions.
14 changes: 13 additions & 1 deletion codegen/method.go
Expand Up @@ -44,6 +44,7 @@ const (
// generated argument directly instead of a struct that warps the argument.
// The annotated method should have one and only one argument.
AntHTTPReqDefBoxed = "%s.http.req.def"
antHTTPResNoBody = "%s.http.res.body.disallow"
)

const queryAnnotationPrefix = "query."
Expand All @@ -62,7 +63,8 @@ type PathSegment struct {
type ExceptionSpec struct {
StructSpec

StatusCode StatusCode
StatusCode StatusCode
IsBodyDisallowed bool
}

// HeaderFieldInfo contains information about where to store
Expand Down Expand Up @@ -162,6 +164,7 @@ type annotations struct {
Meta string
Handler string
HTTPReqDefBoxed string
HTTPResNoBody string
}

// StructSpec specifies a Go struct to be generated.
Expand Down Expand Up @@ -207,6 +210,7 @@ func NewMethod(
Meta: fmt.Sprintf(antMeta, ant),
Handler: fmt.Sprintf(antHandler, ant),
HTTPReqDefBoxed: fmt.Sprintf(AntHTTPReqDefBoxed, ant),
HTTPResNoBody: fmt.Sprintf(antHTTPResNoBody, ant),
}

method.GenCodePkgName, err = packageHelper.TypePackageName(thriftFile)
Expand Down Expand Up @@ -411,12 +415,14 @@ func (ms *MethodSpec) setExceptions(
)
}

bodyDisallowed := ms.isBodyDisallowed(e)
if !ms.WantAnnot {
exception := ExceptionSpec{
StructSpec: StructSpec{
Type: typeName,
Name: e.Name,
},
IsBodyDisallowed: bodyDisallowed,
}
ms.Exceptions[i] = exception
ms.ExceptionsIndex[e.Name] = exception
Expand Down Expand Up @@ -448,6 +454,7 @@ func (ms *MethodSpec) setExceptions(
Code: code,
Message: e.Name,
},
IsBodyDisallowed: bodyDisallowed,
}
ms.Exceptions[i] = exception
ms.ExceptionsIndex[e.Name] = exception
Expand Down Expand Up @@ -1567,6 +1574,11 @@ func (ms *MethodSpec) isRequestBoxed(f *compile.FunctionSpec) bool {
return ok && boxed == "true"
}

func (ms *MethodSpec) isBodyDisallowed(f *compile.FieldSpec) bool {
val, ok := f.Annotations[ms.annotations.HTTPResNoBody]
return ok && val == "true"
}

func headers(annotation string) []string {
if annotation == "" {
return nil
Expand Down
30 changes: 25 additions & 5 deletions codegen/template_bundle/template_files.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions codegen/templates/endpoint.tmpl
Expand Up @@ -207,12 +207,26 @@ func (h *{{$handlerName}}) HandleRequest(
res.SendError(500, "Unexpected server error", err)
return
{{ else }}
{{$val := false}}
{{range $idx, $exception := .Exceptions}}
{{if not $exception.IsBodyDisallowed}}
{{$val = true}}
{{ end}}
{{end}}
{{ if $val -}}
switch errValue := err.(type) {
{{else -}}
switch err.(type) {
{{end -}}
{{range $idx, $exception := .Exceptions}}
case *{{$exception.Type}}:
{{if $exception.IsBodyDisallowed -}}
res.WriteJSONBytes({{$exception.StatusCode.Code}}, cliRespHeaders, nil)
{{else -}}
res.WriteJSON(
{{$exception.StatusCode.Code}}, cliRespHeaders, errValue,
)
{{end -}}
return
{{end}}
default:
Expand Down
12 changes: 9 additions & 3 deletions codegen/templates/http_client.tmpl
Expand Up @@ -337,11 +337,14 @@ func (c *{{$clientName}}) {{$methodName}}(
return respHeaders, nil
{{range $code, $exceptions := .ExceptionsByStatusCode -}}
case {{$code}}:
{{- if or (eq $code 204) (eq $code 304) -}}
{{- if or (eq $code 204) (eq $code 304) }}
{{/* If multiple exceptions have 204/304 status code mapped, we aren't able to distinguish between them */}}
{{/* so we'll just return the first exception that has 204/304 status code set. */}}
{{$val := index $exceptions 0}}
return respHeaders, &{{$val.Type}}{}
{{ else if and (eq (len $exceptions) 1) (eq (index $exceptions 0).IsBodyDisallowed true) -}}
{{$val := index $exceptions 0}}
return respHeaders, &{{$val.Type}}{}
{{else}}
allOptions := []interface{}{
{{range $idx, $exception := $exceptions -}}
Expand All @@ -364,7 +367,7 @@ func (c *{{$clientName}}) {{$methodName}}(
{{else}}
switch res.StatusCode {
case {{.OKStatusCode.Code}}:
{{- if or (eq (.OKStatusCode.Code) 204) (eq (.OKStatusCode.Code) 304) -}}
{{- if or (eq (.OKStatusCode.Code) 204) (eq (.OKStatusCode.Code) 304) }}
return {{if isPointerType .ResponseType}}&{{end}}{{unref .ResponseType}}{}, respHeaders, nil
{{- else }}
{{- if eq .ResponseType "[]byte"}}
Expand All @@ -389,11 +392,14 @@ func (c *{{$clientName}}) {{$methodName}}(
{{end}}
{{range $code, $exceptions := .ExceptionsByStatusCode -}}
case {{$code}}:
{{- if or (eq $code 204) (eq $code 304) -}}
{{- if or (eq $code 204) (eq $code 304) }}
{{/* If multiple exceptions have 204/304 status code mapped, we aren't able to distinguish between them */}}
{{/* so we'll just return the first exception that has 204/304 status code set. */}}
{{$val := index $exceptions 0}}
return defaultRes, respHeaders, &{{$val.Type}}{}
{{ else if and (eq (len $exceptions) 1) (eq (index $exceptions 0).IsBodyDisallowed true) -}}
{{$val := index $exceptions 0}}
return defaultRes, respHeaders, &{{$val.Type}}{}
{{else}}
allOptions := []interface{}{
{{range $idx, $exception := $exceptions -}}
Expand Down
8 changes: 7 additions & 1 deletion examples/example-gateway/build/clients/contacts/contacts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 993a605

Please sign in to comment.