desec: improvement of error logs. (#1161)

This commit is contained in:
Ludovic Fernandez 2020-05-16 20:15:23 +02:00 committed by GitHub
parent e07bf641ab
commit 444e311688
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 25 deletions

View file

@ -102,29 +102,25 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
recordName := getRecordName(fqdn, authZone) recordName := getRecordName(fqdn, authZone)
rrSet, err := d.client.GetTxtRRSet(dns01.UnFqdn(authZone), recordName) domainName := dns01.UnFqdn(authZone)
var nf *internal.NotFound
if err != nil && !errors.As(err, &nf) {
return fmt.Errorf("desec: failed to get records: %w", err)
}
rrSet, err := d.client.GetTxtRRSet(domainName, recordName)
if err != nil { if err != nil {
var nf *internal.NotFound var nf *internal.NotFound
if !errors.As(err, &nf) { 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 // Not found case -> create
_, err = d.client.AddTxtRRSet(internal.RRSet{ _, err = d.client.AddTxtRRSet(internal.RRSet{
Domain: dns01.UnFqdn(authZone), Domain: domainName,
SubName: recordName, SubName: recordName,
Type: "TXT", Type: "TXT",
Records: []string{quotedValue}, Records: []string{quotedValue},
TTL: d.config.TTL, TTL: d.config.TTL,
}) })
if err != nil { 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 return nil
@ -133,9 +129,9 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
// update // update
records := append(rrSet.Records, quotedValue) records := append(rrSet.Records, quotedValue)
_, err = d.client.UpdateTxtRRSet(dns01.UnFqdn(authZone), recordName, records) _, err = d.client.UpdateTxtRRSet(domainName, recordName, records)
if err != nil { 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 return nil
@ -152,9 +148,11 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
recordName := getRecordName(fqdn, authZone) 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 { 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) 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 { 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 return nil

View file

@ -156,16 +156,16 @@ func (c *Client) UpdateTxtRRSet(domainName string, subName string, records []str
return nil, fmt.Errorf("failed to call API: %w", err) 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) // when a RRSet is deleted (empty records)
if resp.StatusCode == http.StatusNoContent { if resp.StatusCode == http.StatusNoContent {
return nil, nil 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 { if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("error: %d: %s", resp.StatusCode, string(body)) 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) 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 { if resp.StatusCode != http.StatusNoContent {
body, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("error: %d: %s", resp.StatusCode, string(body)) return fmt.Errorf("error: %d: %s", resp.StatusCode, string(body))
} }