@@ 20,6 20,11 @@ type ClientCredentials struct {
Password string
}
+type apiError struct {
+ Title string `json:"title"`
+ Description string `json:"description"`
+}
+
func NewMailmanCLient(url string, username string, password string) (*Client, error) {
httpClient := &http.Client{
Timeout: time.Second * 3,
@@ 122,14 127,17 @@ func (client *Client) sendRequest(req *http.Request) ([]byte, error) {
}
defer resp.Body.Close()
- if resp.StatusCode >= 400 {
- return nil, fmt.Errorf("API error code: %d", resp.StatusCode)
- }
-
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
+ if resp.StatusCode >= 400 {
+ var error apiError
+ json.Unmarshal(responseBody, &error)
+
+ return nil, fmt.Errorf("API error %s: %s", error.Title, error.Description)
+ }
+
return responseBody, nil
}