chore(getip): change log messages

- log each error as they happen at the debug level
- return all errors joined if all tries fail and log at the error level
This commit is contained in:
Quentin McGaw
2024-02-01 15:56:12 +00:00
parent b6e8f699a6
commit 443daad0fa
2 changed files with 25 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package update
import (
"context"
"fmt"
"net/netip"
"strconv"
@@ -13,11 +14,13 @@ type getIPFunc func(ctx context.Context) (ip netip.Addr, err error)
func tryAndRepeatGettingIP(ctx context.Context, getIPFunc getIPFunc,
logger Logger, version ipversion.IPVersion) (ip netip.Addr, err error) {
const tries = 3
logMessagePrefix := "obtaining " + version.String() + " address"
logMessagePrefix := "obtaining " + version.String() + " address failed"
errs := make([]error, 0, tries)
for try := 0; try < tries; try++ {
ip, err = getIPFunc(ctx)
if err != nil {
logger.Warn(logMessagePrefix + ": try " + strconv.Itoa(try+1) + " of " +
errs = append(errs, err)
logger.Debug(logMessagePrefix + ": try " + strconv.Itoa(try+1) + " of " +
strconv.Itoa(tries) + ": " + err.Error())
continue
}
@@ -25,7 +28,8 @@ func tryAndRepeatGettingIP(ctx context.Context, getIPFunc getIPFunc,
logger.Info(logMessagePrefix + ": succeeded after " +
strconv.Itoa(try+1) + " tries")
}
break
return ip, nil
}
return ip, err
err = &joinedErrors{errs: errs}
return ip, fmt.Errorf("%s: after %d tries, errors were: %w", logMessagePrefix, tries, err)
}

View File

@@ -3,6 +3,7 @@ package update
import (
"fmt"
"net/netip"
"strings"
"github.com/qdm12/ddns-updater/pkg/publicip/ipversion"
)
@@ -33,3 +34,19 @@ func (r *Runner) logInfoLookupUpdate(hostname, ipKind string, recordIP, ip netip
r.logger.Info(fmt.Sprintf("%s address of %s is %s and your %s address is %s",
ipKind, hostname, recordIP, ipKind, ip))
}
type joinedErrors struct { //nolint:errname
errs []error
}
func (e *joinedErrors) Error() string {
errorMessages := make([]string, len(e.errs))
for i := range e.errs {
errorMessages[i] = e.errs[i].Error()
}
return strings.Join(errorMessages, ", ")
}
func (e *joinedErrors) Unwrap() []error {
return e.errs
}