From 1389afd8d8c42e65e0fb153a66a2d1e351e599fc Mon Sep 17 00:00:00 2001 From: xenolf Date: Thu, 19 May 2016 18:22:40 +0200 Subject: [PATCH] Better cloudflare API error handling. Report all errors if more then one error is available. --- providers/dns/cloudflare/cloudflare.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/providers/dns/cloudflare/cloudflare.go b/providers/dns/cloudflare/cloudflare.go index e566224e..3f869c10 100644 --- a/providers/dns/cloudflare/cloudflare.go +++ b/providers/dns/cloudflare/cloudflare.go @@ -156,8 +156,9 @@ func (c *DNSProvider) findTxtRecord(fqdn string) (*cloudFlareRecord, error) { func (c *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawMessage, error) { // APIError contains error details for failed requests type APIError struct { - Code int `json:"code,omitempty"` - Message string `json:"message,omitempty"` + Code int `json:"code,omitempty"` + Message string `json:"message,omitempty"` + ErrorChain []APIError `json:"error_chain,omitempty"` } // APIResponse represents a response from CloudFlare API @@ -179,7 +180,7 @@ func (c *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawM client := http.Client{Timeout: 30 * time.Second} resp, err := client.Do(req) if err != nil { - return nil, fmt.Errorf("Error querying API -> %v", err) + return nil, fmt.Errorf("Error querying Cloudflare API -> %v", err) } defer resp.Body.Close() @@ -192,9 +193,16 @@ func (c *DNSProvider) makeRequest(method, uri string, body io.Reader) (json.RawM if !r.Success { if len(r.Errors) > 0 { - return nil, fmt.Errorf("API error -> %d: %s", r.Errors[0].Code, r.Errors[0].Message) + errStr := "" + for _, apiErr := range r.Errors { + errStr += fmt.Sprintf("\t Error: %d: %s", apiErr.Code, apiErr.Message) + for _, chainErr := range apiErr.ErrorChain { + errStr += fmt.Sprintf("<- %d: %s", chainErr.Code, chainErr.Message) + } + } + return nil, fmt.Errorf("Cloudflare API Error \n%s", errStr) } - return nil, fmt.Errorf("API error") + return nil, fmt.Errorf("Cloudflare API error") } return r.Result, nil