Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
feat: include API messages in errors (#169)
Browse files Browse the repository at this point in the history
This updates the `formatErrorMessage` helper so that, if the API
responded with an Error model, the messages from that Error model are
appended to the status code & status description. This makes it easier
for users to get visibility into why an API call failed.

I chose to leave the existing code to support rfc7807 messages in place,
just in case we ever migrate to that standard.

Fixes #168
  • Loading branch information
ctreatma committed Nov 2, 2023
1 parent fff5d74 commit 3216493
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
30 changes: 21 additions & 9 deletions metal/v1/client.go

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

30 changes: 21 additions & 9 deletions templates/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -733,20 +733,32 @@ func (e GenericOpenAPIError) Model() interface{} {
return e.model
}

// format error message using title and detail when model implements rfc7807
// format error message using title and detail when model is an instance of
// the Error component schema or when it implements rfc7807
func formatErrorMessage(status string, v interface{}) string {
str := ""
metaValue := reflect.ValueOf(v).Elem()
errorModel, ok := v.(*Error)
if metaValue.Kind() == reflect.Struct {
field := metaValue.FieldByName("Title")
if field != (reflect.Value{}) {
str = fmt.Sprintf("%s", field.Interface())
if ok {
errs := []string{}
errs = append(errs, errorModel.GetErrors()...)
if errorModel.GetError() != "" {
errs = append(errs, errorModel.GetError())
}
str = strings.Join(errs, ", ")
} else {
metaValue := reflect.ValueOf(v).Elem()
if metaValue.Kind() == reflect.Struct {
field := metaValue.FieldByName("Title")
if field != (reflect.Value{}) {
str = fmt.Sprintf("%s", field.Interface())
}
field = metaValue.FieldByName("Detail")
if field != (reflect.Value{}) {
str = fmt.Sprintf("%s (%s)", str, field.Interface())
field = metaValue.FieldByName("Detail")
if field != (reflect.Value{}) {
str = fmt.Sprintf("%s (%s)", str, field.Interface())
}
}
}
Expand Down

0 comments on commit 3216493

Please sign in to comment.