mirror of
https://github.com/qdm12/ddns-updater.git
synced 2026-04-05 00:43:53 -04:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user