Skip to content

Commit

Permalink
Reduce the complexity of ScreenRedfishError
Browse files Browse the repository at this point in the history
Change-Id: I69b7e546cd814f5e3b6b890348f4e808058ae6ef
Relates-To: #142
  • Loading branch information
ian-howell committed Apr 1, 2020
1 parent ddd0f38 commit 68ec25d
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions pkg/remote/redfish/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,32 @@ func (e ErrOperationRetriesExceeded) Error() string {

// ScreenRedfishError provides detailed error checking on a Redfish client response.
func ScreenRedfishError(httpResp *http.Response, clientErr error) error {
if httpResp == nil {
return ErrRedfishClient{Message: "HTTP request failed. Please try again."}
}

// NOTE(drewwalters96): clientErr may not be nil even though the request was successful. The HTTP status code
// has to be verified for success on each request. The Redfish client uses HTTP codes 200 and 204 to indicate
// success.
if httpResp != nil && (httpResp.StatusCode < http.StatusOK || httpResp.StatusCode > http.StatusNoContent) {
if clientErr == nil {
return ErrRedfishClient{Message: http.StatusText(httpResp.StatusCode)}
}
if httpResp.StatusCode >= http.StatusOK && httpResp.StatusCode <= http.StatusNoContent {
// This range of status codes indicate success
return nil
}

oAPIErr, ok := clientErr.(redfishClient.GenericOpenAPIError)
if !ok {
return ErrRedfishClient{Message: "Unable to decode client error."}
}
if clientErr == nil {
return ErrRedfishClient{Message: http.StatusText(httpResp.StatusCode)}
}

var resp redfishClient.RedfishError
if err := json.Unmarshal(oAPIErr.Body(), &resp); err != nil {
// No JSON response included; use generic error text.
return ErrRedfishClient{Message: err.Error()}
}
oAPIErr, ok := clientErr.(redfishClient.GenericOpenAPIError)
if !ok {
return ErrRedfishClient{Message: "Unable to decode client error."}
}

return ErrRedfishClient{Message: resp.Error.Message}
} else if httpResp == nil {
return ErrRedfishClient{Message: "HTTP request failed. Please try again."}
var resp redfishClient.RedfishError
if err := json.Unmarshal(oAPIErr.Body(), &resp); err != nil {
// No JSON response included; use generic error text.
return ErrRedfishClient{Message: err.Error()}
}

return nil
return ErrRedfishClient{Message: resp.Error.Message}
}

0 comments on commit 68ec25d

Please sign in to comment.