From c7338888e41e9faae58d864606c6813f92356a66 Mon Sep 17 00:00:00 2001 From: Matthew R Kasun Date: Fri, 10 Feb 2023 06:58:00 -0500 Subject: [PATCH] error handling --- controllers/hosts.go | 4 ++-- models/dnsEntry.go | 10 ++++++++++ mq/publishers.go | 39 ++++++++++++++------------------------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index d8d350e5..57123071 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -112,9 +112,9 @@ func updateHost(w http.ResponseWriter, r *http.Request) { if newHost.Name != currHost.Name { networks := logic.GetHostNetworks(currHost.ID.String()) if err := mq.PublishHostDNSUpdate(currHost, newHost, networks); err != nil { - var dnsError *mq.DNSError + var dnsError *models.DNSError if errors.Is(err, dnsError) { - for _, message := range err.(mq.DNSError).ErrorStrings { + for _, message := range err.(models.DNSError).ErrorStrings { logger.Log(0, message) } } else { diff --git a/models/dnsEntry.go b/models/dnsEntry.go index 8993c00f..73373f1a 100644 --- a/models/dnsEntry.go +++ b/models/dnsEntry.go @@ -21,6 +21,16 @@ func (action DNSUpdateAction) String() string { return [...]string{"DNSDeleteByIP", "DNSDeletByName", "DNSReplaceName", "DNSReplaceIP", "DNSInsert"}[action] } +// DNSError.Error implementation of error interface +func (e DNSError) Error() string { + return "error publishing dns update" +} + +// DNSError error struct capable of holding multiple error messages +type DNSError struct { + ErrorStrings []string +} + // DNSUpdate data for updating entries in /etc/hosts type DNSUpdate struct { Action DNSUpdateAction diff --git a/mq/publishers.go b/mq/publishers.go index 3179a9d7..79a6ec50 100644 --- a/mq/publishers.go +++ b/mq/publishers.go @@ -296,7 +296,7 @@ func PublishReplaceDNS(oldNode, newNode *models.Node, host *models.Host) error { // PublishExtClientDNS publish dns update for new extclient func PublishExtCLientDNS(client *models.ExtClient) error { - var err4, err6 error + errMsgs := models.DNSError{} dns := models.DNSUpdate{ Action: models.DNSInsert, Name: client.ClientID + "." + client.Network, @@ -304,20 +304,19 @@ func PublishExtCLientDNS(client *models.ExtClient) error { } if client.Address != "" { dns.Address = client.Address - err4 = PublishDNSUpdate(client.Network, dns) + if err := PublishDNSUpdate(client.Network, dns); err != nil { + errMsgs.ErrorStrings = append(errMsgs.ErrorStrings, err.Error()) + } + } if client.Address6 != "" { dns.Address = client.Address6 - err6 = PublishDNSUpdate(client.Network, dns) + if err := PublishDNSUpdate(client.Network, dns); err != nil { + errMsgs.ErrorStrings = append(errMsgs.ErrorStrings, err.Error()) + } } - if err4 != nil && err6 != nil { - return fmt.Errorf("error publishing extclient dns update %w %w", err4, err6) - } - if err4 != nil { - return fmt.Errorf("error publishing extclient dns update %w", err4) - } - if err6 != nil { - return fmt.Errorf("error publishing extclient dns update %w", err6) + if len(errMsgs.ErrorStrings) > 0 { + return errMsgs } return nil } @@ -361,19 +360,9 @@ func PublishCustomDNS(entry *models.DNSEntry) error { return nil } -// DNSError error struct capable of holding multiple error messages -type DNSError struct { - ErrorStrings []string -} - -// DNSError.Error implementation of error interface -func (e DNSError) Error() string { - return "error publishing dns update" -} - // PublishHostDNSUpdate publishes dns update on host name change func PublishHostDNSUpdate(old, new *models.Host, networks []string) error { - errors := DNSError{} + errMsgs := models.DNSError{} for _, network := range networks { dns := models.DNSUpdate{ Action: models.DNSReplaceName, @@ -381,11 +370,11 @@ func PublishHostDNSUpdate(old, new *models.Host, networks []string) error { NewName: new.Name + "." + network, } if err := PublishDNSUpdate(network, dns); err != nil { - errors.ErrorStrings = append(errors.ErrorStrings, err.Error()) + errMsgs.ErrorStrings = append(errMsgs.ErrorStrings, err.Error()) } } - if len(errors.ErrorStrings) > 0 { - return errors + if len(errMsgs.ErrorStrings) > 0 { + return errMsgs } return nil }