fix(client): handle error responses with detailed messages
Adds error handling for non-OK HTTP status codes in the client. Implements custom error messages by reading the response body when the status code indicates an error, ensuring better debugging and clarity during failures. Enhances unit tests to cover unauthorized access and incorrect body length scenarios, validating the error handling mechanism.
This commit is contained in:
@@ -69,6 +69,12 @@ func (r *RestClient) projectApiCall(method, project string, api string, body io.
|
||||
if resp.StatusCode == http.StatusOK && response != nil {
|
||||
decoder := json.NewDecoder(resp.Body)
|
||||
err = decoder.Decode(response)
|
||||
} else if resp.StatusCode != http.StatusOK {
|
||||
buff, err2 := io.ReadAll(resp.Body)
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("error reading body: %w", err2)
|
||||
}
|
||||
return fmt.Errorf("status %d: %s", resp.StatusCode, string(buff))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user