Skip to content

Commit

Permalink
errJson -> errJSON and update proto field access
Browse files Browse the repository at this point in the history
Signed-off-by: Cassandra Coyle <[email protected]>
  • Loading branch information
cicoyle committed Dec 11, 2023
1 parent e90c3bd commit 5e5d02f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
40 changes: 21 additions & 19 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (e *Error) WithHelpLink(url string, description string) *Error {

return e
}

func (e *Error) WithHelp(links []*errdetails.Help_Link) *Error {
e.Details = append(e.Details, &errdetails.Help{Links: links})

Expand Down Expand Up @@ -134,6 +135,7 @@ func (e *Error) WithFieldViolation(fieldName string, msg string) *Error {

return e
}

func (e *Error) WithDetails(details ...proto.Message) *Error {
e.Details = append(e.Details, details...)

Expand Down Expand Up @@ -188,15 +190,15 @@ func (e *Error) JSONErrorValue() []byte {
httpStatus = http.StatusText(e.HttpCode)
}

errJson := ErrorJSON{
errJSON := ErrorJSON{
ErrorCode: httpStatus,
Message: grpcStatus.GetMessage(),
}

// Handle err details
details := e.Details
if len(details) > 0 {
errJson.Details = make([]any, len(details))
errJSON.Details = make([]any, len(details))
for i, detail := range details {
// cast to interface to be able to do type switch
// over all possible error_details defined
Expand All @@ -210,56 +212,56 @@ func (e *Error) JSONErrorValue() []byte {
"domain": typedDetail.GetDomain(),
"metadata": typedDetail.GetMetadata(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap

// If there is an ErrorInfo Reason, but no legacy Tag code, use the ErrorInfo Reason as the error code
if e.Tag == "" && typedDetail.GetReason() != "" {
errJson.ErrorCode = typedDetail.GetReason()
errJSON.ErrorCode = typedDetail.GetReason()
}
case *errdetails.RetryInfo:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"retry_delay": typedDetail.GetRetryDelay(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.DebugInfo:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"stack_entries": typedDetail.GetStackEntries(),
"detail": typedDetail.GetDetail(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.QuotaFailure:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"violations": typedDetail.GetViolations(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.PreconditionFailure:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"violations": typedDetail.GetViolations(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.BadRequest:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"field_violations": typedDetail.GetFieldViolations(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.RequestInfo:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"request_id": typedDetail.GetRequestId(),
"serving_data": typedDetail.GetServingData(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.ResourceInfo:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
Expand All @@ -269,30 +271,30 @@ func (e *Error) JSONErrorValue() []byte {
"owner": typedDetail.GetOwner(),
"description": typedDetail.GetDescription(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.Help:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"links": typedDetail.GetLinks(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.LocalizedMessage:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"locale": typedDetail.GetLocale(),
"message": typedDetail.GetMessage(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.QuotaFailure_Violation:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"subject": typedDetail.GetSubject(),
"description": typedDetail.GetDescription(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.PreconditionFailure_Violation:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
Expand All @@ -301,36 +303,36 @@ func (e *Error) JSONErrorValue() []byte {
"description": typedDetail.GetDescription(),
"type": typedDetail.GetType(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.BadRequest_FieldViolation:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"field": typedDetail.GetField(),
"description": typedDetail.GetDescription(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
case *errdetails.Help_Link:
desc := typedDetail.ProtoReflect().Descriptor()
detailMap := map[string]interface{}{
"@type": typeGoogleAPI + desc.FullName(),
"description": typedDetail.GetDescription(),
"url": typedDetail.GetUrl(),
}
errJson.Details[i] = detailMap
errJSON.Details[i] = detailMap
default:
log.Debugf("Failed to convert error details due to incorrect type. \nSee types here: https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto. \nDetail: %s", detail)
// Handle unknown detail types
unknownDetail := map[string]interface{}{
"unknownDetailType": fmt.Sprintf("%T", typedDetail),
"unknownDetails": fmt.Sprintf("%#v", typedDetail),
}
errJson.Details[i] = unknownDetail
errJSON.Details[i] = unknownDetail
}
}
}

errBytes, err := json.Marshal(errJson)
errBytes, err := json.Marshal(errJSON)
if err != nil {
errJSON, _ := json.Marshal(fmt.Sprintf("failed to encode proto to JSON: %v", err))
return errJSON
Expand Down
2 changes: 1 addition & 1 deletion errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func TestWithErrorHelp(t *testing.T) {
// Type assert to *errdetails.Help
helpDetail, ok := err.Details[0].(*errdetails.Help)
require.True(t, ok, "Details[0] should be of type *errdetails.Help")
require.Equal(t, links, helpDetail.Links, "Links should match the provided links")
require.Equal(t, links, helpDetail.GetLinks(), "Links should match the provided links")
}

func TestWithErrorFieldViolation(t *testing.T) {
Expand Down

0 comments on commit 5e5d02f

Please sign in to comment.