error handling

This commit is contained in:
Matthew R Kasun 2023-02-10 06:58:00 -05:00
parent 98e66b7292
commit c7338888e4
3 changed files with 26 additions and 27 deletions

View file

@ -112,9 +112,9 @@ func updateHost(w http.ResponseWriter, r *http.Request) {
if newHost.Name != currHost.Name { if newHost.Name != currHost.Name {
networks := logic.GetHostNetworks(currHost.ID.String()) networks := logic.GetHostNetworks(currHost.ID.String())
if err := mq.PublishHostDNSUpdate(currHost, newHost, networks); err != nil { if err := mq.PublishHostDNSUpdate(currHost, newHost, networks); err != nil {
var dnsError *mq.DNSError var dnsError *models.DNSError
if errors.Is(err, dnsError) { if errors.Is(err, dnsError) {
for _, message := range err.(mq.DNSError).ErrorStrings { for _, message := range err.(models.DNSError).ErrorStrings {
logger.Log(0, message) logger.Log(0, message)
} }
} else { } else {

View file

@ -21,6 +21,16 @@ func (action DNSUpdateAction) String() string {
return [...]string{"DNSDeleteByIP", "DNSDeletByName", "DNSReplaceName", "DNSReplaceIP", "DNSInsert"}[action] 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 // DNSUpdate data for updating entries in /etc/hosts
type DNSUpdate struct { type DNSUpdate struct {
Action DNSUpdateAction Action DNSUpdateAction

View file

@ -296,7 +296,7 @@ func PublishReplaceDNS(oldNode, newNode *models.Node, host *models.Host) error {
// PublishExtClientDNS publish dns update for new extclient // PublishExtClientDNS publish dns update for new extclient
func PublishExtCLientDNS(client *models.ExtClient) error { func PublishExtCLientDNS(client *models.ExtClient) error {
var err4, err6 error errMsgs := models.DNSError{}
dns := models.DNSUpdate{ dns := models.DNSUpdate{
Action: models.DNSInsert, Action: models.DNSInsert,
Name: client.ClientID + "." + client.Network, Name: client.ClientID + "." + client.Network,
@ -304,20 +304,19 @@ func PublishExtCLientDNS(client *models.ExtClient) error {
} }
if client.Address != "" { if client.Address != "" {
dns.Address = 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 != "" { if client.Address6 != "" {
dns.Address = 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 { if len(errMsgs.ErrorStrings) > 0 {
return fmt.Errorf("error publishing extclient dns update %w", err4) return errMsgs
}
if err6 != nil {
return fmt.Errorf("error publishing extclient dns update %w", err6)
} }
return nil return nil
} }
@ -361,19 +360,9 @@ func PublishCustomDNS(entry *models.DNSEntry) error {
return nil 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 // PublishHostDNSUpdate publishes dns update on host name change
func PublishHostDNSUpdate(old, new *models.Host, networks []string) error { func PublishHostDNSUpdate(old, new *models.Host, networks []string) error {
errors := DNSError{} errMsgs := models.DNSError{}
for _, network := range networks { for _, network := range networks {
dns := models.DNSUpdate{ dns := models.DNSUpdate{
Action: models.DNSReplaceName, Action: models.DNSReplaceName,
@ -381,11 +370,11 @@ func PublishHostDNSUpdate(old, new *models.Host, networks []string) error {
NewName: new.Name + "." + network, NewName: new.Name + "." + network,
} }
if err := PublishDNSUpdate(network, dns); err != nil { 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 { if len(errMsgs.ErrorStrings) > 0 {
return errors return errMsgs
} }
return nil return nil
} }