1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

Switch to Go 1.13 error wrapping (#604)

* Replaced errors.Wrap with fmt.Errorf (#589)

* Find:    errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
  Replace: fmt.Errorf($2: %w$3, $1)

* Replaced errors.Wrapf with fmt.Errorf (#589)

* Find:    errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
  Replace: fmt.Errorf($2: %w$3, $1)
* Find:    errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)

* Replaced errors.Errorf with fmt.Errorf (#589)

* Find:    errors\.Errorf
  Replace: fmt.Errorf

* Cleaned up remaining imports

* Cleanup

* Regenerate provider support matrix

This was broken by #533 ... and it's now the third time this has been missed.
This commit is contained in:
Patrick Gaskin
2020-01-28 11:06:56 -05:00
committed by Tom Limoncelli
parent cae35a2c8f
commit 825ba2d081
64 changed files with 328 additions and 379 deletions

View File

@ -10,13 +10,12 @@ import (
"github.com/StackExchange/dnscontrol/v2/providers"
"github.com/miekg/dns"
"github.com/miekg/dns/dnsutil"
"github.com/pkg/errors"
)
// Returns false if target does not validate.
func checkIPv4(label string) error {
if net.ParseIP(label).To4() == nil {
return errors.Errorf("WARNING: target (%v) is not an IPv4 address", label)
return fmt.Errorf("WARNING: target (%v) is not an IPv4 address", label)
}
return nil
}
@ -24,7 +23,7 @@ func checkIPv4(label string) error {
// Returns false if target does not validate.
func checkIPv6(label string) error {
if net.ParseIP(label).To16() == nil {
return errors.Errorf("WARNING: target (%v) is not an IPv6 address", label)
return fmt.Errorf("WARNING: target (%v) is not an IPv6 address", label)
}
return nil
}
@ -35,14 +34,14 @@ func checkTarget(target string) error {
return nil
}
if len(target) < 1 {
return errors.Errorf("empty target")
return fmt.Errorf("empty target")
}
if strings.ContainsAny(target, `'" +,|!£$%&/()=?^*ç°§;:<>[]()@`) {
return errors.Errorf("target (%v) includes invalid char", target)
return fmt.Errorf("target (%v) includes invalid char", target)
}
// If it containts a ".", it must end in a ".".
if strings.ContainsRune(target, '.') && target[len(target)-1] != '.' {
return errors.Errorf("target (%v) must end with a (.) [https://stackexchange.github.io/dnscontrol/why-the-dot]", target)
return fmt.Errorf("target (%v) must end with a (.) [https://stackexchange.github.io/dnscontrol/why-the-dot]", target)
}
return nil
}
@ -69,11 +68,11 @@ func validateRecordTypes(rec *models.RecordConfig, domain string, pTypes []strin
if !ok {
cType := providers.GetCustomRecordType(rec.Type)
if cType == nil {
return errors.Errorf("Unsupported record type (%v) domain=%v name=%v", rec.Type, domain, rec.GetLabel())
return fmt.Errorf("Unsupported record type (%v) domain=%v name=%v", rec.Type, domain, rec.GetLabel())
}
for _, providerType := range pTypes {
if providerType != cType.Provider {
return errors.Errorf("Custom record type %s is not compatible with provider type %s", rec.Type, providerType)
return fmt.Errorf("Custom record type %s is not compatible with provider type %s", rec.Type, providerType)
}
}
// it is ok. Lets replace the type with real type and add metadata to say we checked it
@ -105,14 +104,14 @@ func checkLabel(label string, rType string, domain string, meta map[string]strin
return nil
}
if len(label) < 1 {
return errors.Errorf("empty %s label in %s", rType, domain)
return fmt.Errorf("empty %s label in %s", rType, domain)
}
if label[len(label)-1] == '.' {
return errors.Errorf("label %s.%s ends with a (.)", label, domain)
return fmt.Errorf("label %s.%s ends with a (.)", label, domain)
}
if strings.HasSuffix(label, domain) {
if m := meta["skip_fqdn_check"]; m != "true" {
return errors.Errorf(`label %s ends with domain name %s. Record names should not be fully qualified. Add {skip_fqdn_check:"true"} to this record if you really want to make %s.%s`, label, domain, label, domain)
return fmt.Errorf(`label %s ends with domain name %s. Record names should not be fully qualified. Add {skip_fqdn_check:"true"} to this record if you really want to make %s.%s`, label, domain, label, domain)
}
}
@ -133,7 +132,7 @@ func checkLabel(label string, rType string, domain string, meta map[string]strin
}
// Otherwise, warn.
if strings.ContainsRune(label, '_') {
return Warning{errors.Errorf("label %s.%s contains an underscore", label, domain)}
return Warning{fmt.Errorf("label %s.%s contains an underscore", label, domain)}
}
return nil
@ -145,7 +144,7 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
target := rec.GetTargetField()
check := func(e error) {
if e != nil {
err := errors.Errorf("In %s %s.%s: %s", rec.Type, rec.GetLabel(), domain, e.Error())
err := fmt.Errorf("In %s %s.%s: %s", rec.Type, rec.GetLabel(), domain, e.Error())
if _, ok := e.(Warning); ok {
err = Warning{err}
}
@ -160,14 +159,14 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
case "CNAME":
check(checkTarget(target))
if label == "@" {
check(errors.Errorf("cannot create CNAME record for bare domain"))
check(fmt.Errorf("cannot create CNAME record for bare domain"))
}
case "MX":
check(checkTarget(target))
case "NS":
check(checkTarget(target))
if label == "@" {
check(errors.Errorf("cannot create NS record for bare domain. Use NAMESERVER instead"))
check(fmt.Errorf("cannot create NS record for bare domain. Use NAMESERVER instead"))
}
case "PTR":
check(checkTarget(target))
@ -183,7 +182,7 @@ func checkTargets(rec *models.RecordConfig, domain string) (errs []error) {
// it is a valid custom type. We perform no validation on target
return
}
errs = append(errs, errors.Errorf("checkTargets: Unimplemented record type (%v) domain=%v name=%v",
errs = append(errs, fmt.Errorf("checkTargets: Unimplemented record type (%v) domain=%v name=%v",
rec.Type, domain, rec.GetLabel()))
}
return
@ -223,7 +222,7 @@ func importTransform(srcDomain, dstDomain *models.DomainConfig, transforms []tra
case "A":
trs, err := transform.TransformIPToList(net.ParseIP(rec.GetTargetField()), transforms)
if err != nil {
return errors.Errorf("import_transform: TransformIP(%v, %v) returned err=%s", rec.GetTargetField(), transforms, err)
return fmt.Errorf("import_transform: TransformIP(%v, %v) returned err=%s", rec.GetTargetField(), transforms, err)
}
for _, tr := range trs {
r := newRec()
@ -238,7 +237,7 @@ func importTransform(srcDomain, dstDomain *models.DomainConfig, transforms []tra
// Not imported.
continue
default:
return errors.Errorf("import_transform: Unimplemented record type %v (%v)",
return fmt.Errorf("import_transform: Unimplemented record type %v (%v)",
rec.Type, rec.GetLabel())
}
}
@ -270,7 +269,7 @@ func NormalizeAndValidateConfig(config *models.DNSConfig) (errs []error) {
pType := provider.ProviderType
// If NO_PURGE is in use, make sure this *isn't* a provider that *doesn't* support NO_PURGE.
if domain.KeepUnknown && providers.ProviderHasCapability(pType, providers.CantUseNOPURGE) {
errs = append(errs, errors.Errorf("%s uses NO_PURGE which is not supported by %s(%s)", domain.Name, provider.Name, pType))
errs = append(errs, fmt.Errorf("%s uses NO_PURGE which is not supported by %s(%s)", domain.Name, provider.Name, pType))
}
// Record if any providers do not support TXTMulti:
@ -319,26 +318,26 @@ func NormalizeAndValidateConfig(config *models.DNSConfig) (errs []error) {
rec.SetLabel(name, domain.Name)
} else if rec.Type == "CAA" {
if rec.CaaTag != "issue" && rec.CaaTag != "issuewild" && rec.CaaTag != "iodef" {
errs = append(errs, errors.Errorf("CAA tag %s is invalid", rec.CaaTag))
errs = append(errs, fmt.Errorf("CAA tag %s is invalid", rec.CaaTag))
}
} else if rec.Type == "TLSA" {
if rec.TlsaUsage < 0 || rec.TlsaUsage > 3 {
errs = append(errs, errors.Errorf("TLSA Usage %d is invalid in record %s (domain %s)",
errs = append(errs, fmt.Errorf("TLSA Usage %d is invalid in record %s (domain %s)",
rec.TlsaUsage, rec.GetLabel(), domain.Name))
}
if rec.TlsaSelector < 0 || rec.TlsaSelector > 1 {
errs = append(errs, errors.Errorf("TLSA Selector %d is invalid in record %s (domain %s)",
errs = append(errs, fmt.Errorf("TLSA Selector %d is invalid in record %s (domain %s)",
rec.TlsaSelector, rec.GetLabel(), domain.Name))
}
if rec.TlsaMatchingType < 0 || rec.TlsaMatchingType > 2 {
errs = append(errs, errors.Errorf("TLSA MatchingType %d is invalid in record %s (domain %s)",
errs = append(errs, fmt.Errorf("TLSA MatchingType %d is invalid in record %s (domain %s)",
rec.TlsaMatchingType, rec.GetLabel(), domain.Name))
}
} else if rec.Type == "TXT" && len(txtMultiDissenters) != 0 && len(rec.TxtStrings) > 1 {
// There are providers that don't support TXTMulti yet there is
// a TXT record with multiple strings:
errs = append(errs,
errors.Errorf("TXT records with multiple strings (label %v domain: %v) not supported by %s",
fmt.Errorf("TXT records with multiple strings (label %v domain: %v) not supported by %s",
rec.GetLabel(), domain.Name, strings.Join(txtMultiDissenters, ",")))
}
@ -405,14 +404,14 @@ func checkCNAMEs(dc *models.DomainConfig) (errs []error) {
for _, r := range dc.Records {
if r.Type == "CNAME" {
if cnames[r.GetLabel()] {
errs = append(errs, errors.Errorf("Cannot have multiple CNAMEs with same name: %s", r.GetLabelFQDN()))
errs = append(errs, fmt.Errorf("Cannot have multiple CNAMEs with same name: %s", r.GetLabelFQDN()))
}
cnames[r.GetLabel()] = true
}
}
for _, r := range dc.Records {
if cnames[r.GetLabel()] && r.Type != "CNAME" {
errs = append(errs, errors.Errorf("Cannot have CNAME and %s record with same name: %s", r.Type, r.GetLabelFQDN()))
errs = append(errs, fmt.Errorf("Cannot have CNAME and %s record with same name: %s", r.Type, r.GetLabelFQDN()))
}
}
return
@ -423,7 +422,7 @@ func checkDuplicates(records []*models.RecordConfig) (errs []error) {
for _, r := range records {
diffable := fmt.Sprintf("%s %s %s", r.GetLabelFQDN(), r.Type, r.ToDiffable())
if seen[diffable] != nil {
errs = append(errs, errors.Errorf("Exact duplicate record found: %s", diffable))
errs = append(errs, fmt.Errorf("Exact duplicate record found: %s", diffable))
}
seen[diffable] = r
}
@ -454,7 +453,7 @@ func checkProviderCapabilities(dc *models.DomainConfig) error {
}
for _, provider := range dc.DNSProviderInstances {
if !providers.ProviderHasCapability(provider.ProviderType, ty.cap) {
return errors.Errorf("Domain %s uses %s records, but DNS provider type %s does not support them", dc.Name, ty.rType, provider.ProviderType)
return fmt.Errorf("Domain %s uses %s records, but DNS provider type %s does not support them", dc.Name, ty.rType, provider.ProviderType)
}
}
}