~xenrox/terraform-provider-mailman

932a0a24ec2fca073052a3815000f757317cea7d — Thorben Günther 1 year, 1 month ago e04a26d master
client: Improve error handling

Get error json struct from API.
1 files changed, 12 insertions(+), 4 deletions(-)

M mailman/mailman_client.go
M mailman/mailman_client.go => mailman/mailman_client.go +12 -4
@@ 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
}