Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions codegen/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
// The annotated method should have one and only one argument.
AntHTTPReqDefBoxed = "%s.http.req.def"
antHTTPResNoBody = "%s.http.res.body.disallow"
// RPCCode is a rpc code type that allows to read the annotation in thrift and convert to a corresponding header
RPCCode = "rpc.Code"
)

const queryAnnotationPrefix = "query."
Expand All @@ -65,6 +67,8 @@ type ExceptionSpec struct {

StatusCode StatusCode
IsBodyDisallowed bool
IsRPCCodeSet bool
RPCCodeValue string
}

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

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

Expand Down Expand Up @@ -416,14 +422,19 @@ func (ms *MethodSpec) setExceptions(
}

bodyDisallowed := ms.isBodyDisallowed(e)
rpcSet := ms.isRPCHeaderSet(e)
rpcValue := ms.extractRPCCodeValue(e)
if !ms.WantAnnot {
exception := ExceptionSpec{
StructSpec: StructSpec{
Type: typeName,
Name: e.Name,
},
IsBodyDisallowed: bodyDisallowed,
RPCCodeValue: rpcValue,
IsRPCCodeSet: rpcSet,
}

ms.Exceptions[i] = exception
ms.ExceptionsIndex[e.Name] = exception
if _, exists := ms.ExceptionsByStatusCode[exception.StatusCode.Code]; !exists {
Expand Down Expand Up @@ -455,6 +466,8 @@ func (ms *MethodSpec) setExceptions(
Message: e.Name,
},
IsBodyDisallowed: bodyDisallowed,
IsRPCCodeSet: rpcSet,
RPCCodeValue: rpcValue,
}
ms.Exceptions[i] = exception
ms.ExceptionsIndex[e.Name] = exception
Expand Down Expand Up @@ -1580,6 +1593,15 @@ func (ms *MethodSpec) isBodyDisallowed(f *compile.FieldSpec) bool {
return ok && val == "true"
}

func (ms *MethodSpec) isRPCHeaderSet(f *compile.FieldSpec) bool {
_, ok := f.Annotations[RPCCode]
return ok
}

func (ms *MethodSpec) extractRPCCodeValue(f *compile.FieldSpec) string {
return f.Annotations[RPCCode]
}

func headers(annotation string) []string {
if annotation == "" {
return nil
Expand Down
3 changes: 3 additions & 0 deletions codegen/templates/endpoint.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ func (h *{{$handlerName}}) HandleRequest(
{{end -}}
{{range $idx, $exception := .Exceptions}}
case *{{$exception.Type}}:
{{if $exception.IsRPCCodeSet -}}
cliRespHeaders.Add("$rpc$-application-error-code", "{{$exception.RPCCodeValue}}")
{{end -}}
{{if $exception.IsBodyDisallowed -}}
res.WriteJSONBytes({{$exception.StatusCode.Code}}, cliRespHeaders, nil)
{{else -}}
Expand Down
7 changes: 5 additions & 2 deletions codegen/templates/tchannel_endpoint.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
{{- $clientID := .ClientID }}

{{with .Method -}}
// New{{$handlerName}} creates a handler to be registered with a thrift server.
// New{{$handlerName}} creates a simple handler to be registered with a thrift server.
func New{{$handlerName}}(deps *module.Dependencies) *{{$handlerName}} {
handler := &{{$handlerName}}{
Deps: deps,
Expand Down Expand Up @@ -159,8 +159,11 @@ func (h *{{$handlerName}}) Handle(
if err != nil {
switch v := err.(type) {
{{$method := .Name -}}
{{range .Exceptions -}}
{{range $idx, $exception := .Exceptions}}
case *{{.Type}}:
{{if $exception.IsRPCCodeSet -}}
resHeaders["$rpc$-application-error-code"] = "{{$exception.RPCCodeValue}}"
{{end -}}
ctxWithError := zanzibar.WithScopeTags(ctx, map[string]string{
"app-error": "{{.Type}}",
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ service Bar {
string helloWorld(
) throws (
1: BarException barException (zanzibar.http.status = "403")
2: SeeOthersRedirection seeOthersRedirection (zanzibar.http.status = "303", zanzibar.http.res.body.disallow = "true")
2: SeeOthersRedirection seeOthersRedirection (zanzibar.http.status = "303", zanzibar.http.res.body.disallow = "true", rpc.Code = "UNAUTHORIZED")
) (
zanzibar.http.method = "GET"
zanzibar.http.path = "/bar/hello"
Expand Down