Skip to content

Commit

Permalink
fix(errors): Prevent error parsing from crashing
Browse files Browse the repository at this point in the history
Although errors from the service should be returned in a specific
format, we shouldn't rely on this. Istead of unmarshaling directly into
a struct, use an interface that we can use to assert on the fields we
want.

Ref: LOG-20105
  • Loading branch information
darinspivey committed Jun 12, 2024
1 parent 3299842 commit 424c458
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions internal/client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,43 @@ func newAPIError(status int, bodyBuffer []byte, _ context.Context) ApiResponseEr
if len(bodyBuffer) == 0 {
return result
}
err := json.Unmarshal(bodyBuffer, &result)
if err != nil {
result.Message = fmt.Sprintf("failed to decode JSON body. reason: %s", err.Error())
// Errors *should* follow the expected format, but to be safe, we'll unmarshal into an
// interface and assert the fields we expect.
var e map[string]any

if err := json.Unmarshal(bodyBuffer, &e); err != nil {
result.Code = "EUNKNOWN"
result.Message = "There was an error, but the response from the server was not understood."
result.Errors = append(result.Errors, validationError{
Path: "raw error body",
Message: string(bodyBuffer),
})
return result
}

if code, ok := e["code"].(string); ok {
result.Code = code
}
if message, ok := e["message"].(string); ok {
result.Message = message
}
if errors, ok := e["errors"].([]any); ok {
for _, err := range errors {
if e, ok := err.(map[string]any); ok {
vError := validationError{}
if path, ok := e["instancePath"].(string); ok {
vError.Path = path
}
if message, ok := e["message"].(string); ok {
vError.Message = message
}
result.Errors = append(result.Errors, vError)
}
}
} else if errorString, ok := e["errors"].(string); ok {
result.Errors = append(result.Errors, validationError{
Message: errorString,
})
}
return result
}
Expand Down Expand Up @@ -80,6 +114,8 @@ func SkipThisError(msg string) bool {
return true
case "must match a schema in allOf":
return true
case "must match a schema in oneOf":
return true
default:
return false
}
Expand Down

0 comments on commit 424c458

Please sign in to comment.