Skip to content

Commit

Permalink
feat(monitor): prioritize error messages (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Oct 24, 2023
1 parent f1793ad commit 2f653ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ func detectIP(ctx context.Context, ppfmt pp.PP,
// UpdateIPs detect IP addresses and update DNS records of managed domains.
func UpdateIPs(ctx context.Context, ppfmt pp.PP, c *config.Config, s setter.Setter) (bool, string) {
allOk := true
var msgs []string

// [msgs[false]] collects all the error messages and [msgs[true]] collects all the success messages.
msgs := map[bool][]string{}

for _, ipNet := range [...]ipnet.Type{ipnet.IP4, ipnet.IP6} {
if c.Provider[ipNet] != nil {
ip, ok, msg := detectIP(ctx, ppfmt, c, ipNet, c.Use1001)
if msg != "" {
msgs = append(msgs, msg)
if len(msg) != 0 {
msgs[ok] = append(msgs[ok], msg)
}
if !ok {
// We can't detect the new IP address. It's probably better to leave existing IP addresses alone.
Expand All @@ -125,14 +127,20 @@ func UpdateIPs(ctx context.Context, ppfmt pp.PP, c *config.Config, s setter.Sett
}

ok, msg = setIP(ctx, ppfmt, c, s, ipNet, ip)
if msg != "" {
msgs = append(msgs, msg)
if len(msg) != 0 {
msgs[ok] = append(msgs[ok], msg)
}
allOk = allOk && ok
}
}

return allOk, strings.Join(msgs, "\n")
var allMsg string
if len(msgs[false]) != 0 {
allMsg = strings.Join(msgs[false], "\n")
} else {
allMsg = strings.Join(msgs[true], "\n")
}
return allOk, allMsg
}

// ClearIPs removes all DNS records of managed domains.
Expand Down
4 changes: 2 additions & 2 deletions internal/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestUpdateIPs(t *testing.T) {
api.TTLAuto,
proxiedBoth,
false,
"Failed to detect the IPv4 address\nlooking good",
"Failed to detect the IPv4 address",
map[ipnet.Type]bool{ipnet.IP4: true, ipnet.IP6: true},
func(m *mocks.MockPP) {
gomock.InOrder(
Expand All @@ -183,7 +183,7 @@ func TestUpdateIPs(t *testing.T) {
api.TTLAuto,
proxiedNone,
false,
"good\nFailed to detect the IPv6 address",
"Failed to detect the IPv6 address",
map[ipnet.Type]bool{ipnet.IP4: true, ipnet.IP6: true},
func(m *mocks.MockPP) {
gomock.InOrder(
Expand Down

0 comments on commit 2f653ca

Please sign in to comment.