From 444e311688fc391b05aa88cfd5bc095989699dc1 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Sat, 16 May 2020 20:15:23 +0200 Subject: [PATCH] desec: improvement of error logs. (#1161) --- providers/dns/desec/desec.go | 28 ++++++++++++-------------- providers/dns/desec/internal/client.go | 16 ++++++--------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/providers/dns/desec/desec.go b/providers/dns/desec/desec.go index 87db9070..def3b9fe 100644 --- a/providers/dns/desec/desec.go +++ b/providers/dns/desec/desec.go @@ -102,29 +102,25 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { recordName := getRecordName(fqdn, authZone) - rrSet, err := d.client.GetTxtRRSet(dns01.UnFqdn(authZone), recordName) - - var nf *internal.NotFound - if err != nil && !errors.As(err, &nf) { - return fmt.Errorf("desec: failed to get records: %w", err) - } + domainName := dns01.UnFqdn(authZone) + rrSet, err := d.client.GetTxtRRSet(domainName, recordName) if err != nil { var nf *internal.NotFound if !errors.As(err, &nf) { - return fmt.Errorf("desec: failed to get records: %w", err) + return fmt.Errorf("desec: failed to get records: domainName=%s, recordName=%s: %w", domainName, recordName, err) } // Not found case -> create _, err = d.client.AddTxtRRSet(internal.RRSet{ - Domain: dns01.UnFqdn(authZone), + Domain: domainName, SubName: recordName, Type: "TXT", Records: []string{quotedValue}, TTL: d.config.TTL, }) if err != nil { - return fmt.Errorf("desec: failed to create records: %w", err) + return fmt.Errorf("desec: failed to create records: domainName=%s, recordName=%s: %w", domainName, recordName, err) } return nil @@ -133,9 +129,9 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error { // update records := append(rrSet.Records, quotedValue) - _, err = d.client.UpdateTxtRRSet(dns01.UnFqdn(authZone), recordName, records) + _, err = d.client.UpdateTxtRRSet(domainName, recordName, records) if err != nil { - return fmt.Errorf("desec: failed to update records: %w", err) + return fmt.Errorf("desec: failed to update records: domainName=%s, recordName=%s: %w", domainName, recordName, err) } return nil @@ -152,9 +148,11 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { recordName := getRecordName(fqdn, authZone) - rrSet, err := d.client.GetTxtRRSet(dns01.UnFqdn(authZone), recordName) + domainName := dns01.UnFqdn(authZone) + + rrSet, err := d.client.GetTxtRRSet(domainName, recordName) if err != nil { - return fmt.Errorf("desec: failed to create records: %w", err) + return fmt.Errorf("desec: failed to get records: domainName=%s, recordName=%s: %w", domainName, recordName, err) } records := make([]string, 0) @@ -164,9 +162,9 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { } } - _, err = d.client.UpdateTxtRRSet(dns01.UnFqdn(authZone), recordName, records) + _, err = d.client.UpdateTxtRRSet(domainName, recordName, records) if err != nil { - return fmt.Errorf("desec: failed to update records: %w", err) + return fmt.Errorf("desec: failed to update records: domainName=%s, recordName=%s: %w", domainName, recordName, err) } return nil diff --git a/providers/dns/desec/internal/client.go b/providers/dns/desec/internal/client.go index cbd260bd..97be6fa2 100644 --- a/providers/dns/desec/internal/client.go +++ b/providers/dns/desec/internal/client.go @@ -156,16 +156,16 @@ func (c *Client) UpdateTxtRRSet(domainName string, subName string, records []str return nil, fmt.Errorf("failed to call API: %w", err) } - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, fmt.Errorf("failed to read response body: %w", err) - } - // when a RRSet is deleted (empty records) if resp.StatusCode == http.StatusNoContent { return nil, nil } + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("error: %d: %s", resp.StatusCode, string(body)) } @@ -204,12 +204,8 @@ func (c *Client) DeleteTxtRRSet(domainName string, subName string) error { return fmt.Errorf("failed to call API: %w", err) } - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return fmt.Errorf("failed to read response body: %w", err) - } - if resp.StatusCode != http.StatusNoContent { + body, _ := ioutil.ReadAll(resp.Body) return fmt.Errorf("error: %d: %s", resp.StatusCode, string(body)) }