in codegen/method.go [395:470]
func (ms *MethodSpec) setExceptions(
curThriftFile string,
isEndpoint bool,
resultSpec *compile.ResultSpec,
h *PackageHelper,
) error {
ms.Exceptions = make([]ExceptionSpec, len(resultSpec.Exceptions))
ms.ExceptionsIndex = make(
map[string]ExceptionSpec, len(resultSpec.Exceptions),
)
ms.ExceptionsByStatusCode = map[int][]ExceptionSpec{}
for i, e := range resultSpec.Exceptions {
typeName, err := h.TypeFullName(e.Type)
if err != nil {
return errors.Wrapf(
err,
"cannot resolve type full name for %s for exception %s",
e.Type,
e.Name,
)
}
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
if _, exists := ms.ExceptionsByStatusCode[exception.StatusCode.Code]; !exists {
ms.ExceptionsByStatusCode[exception.StatusCode.Code] = []ExceptionSpec{}
}
ms.ExceptionsByStatusCode[exception.StatusCode.Code] = append(
ms.ExceptionsByStatusCode[exception.StatusCode.Code],
exception,
)
continue
}
code, err := strconv.Atoi(e.Annotations[ms.annotations.HTTPStatus])
if err != nil {
return errors.Wrapf(
err,
"cannot parse the annotation %s for exception %s", ms.annotations.HTTPStatus, e.Name,
)
}
exception := ExceptionSpec{
StructSpec: StructSpec{
Type: typeName,
Name: e.Name,
Annotations: e.Annotations,
},
StatusCode: StatusCode{
Code: code,
Message: e.Name,
},
IsBodyDisallowed: bodyDisallowed,
}
ms.Exceptions[i] = exception
ms.ExceptionsIndex[e.Name] = exception
if _, exists := ms.ExceptionsByStatusCode[exception.StatusCode.Code]; !exists {
ms.ExceptionsByStatusCode[exception.StatusCode.Code] = []ExceptionSpec{}
}
ms.ExceptionsByStatusCode[exception.StatusCode.Code] = append(
ms.ExceptionsByStatusCode[exception.StatusCode.Code],
exception,
)
}
return nil
}