Use eris for error management

This commit is contained in:
Jamie Curnow
2023-02-24 17:19:07 +10:00
parent 80315bd50e
commit c288886fd4
44 changed files with 173 additions and 128 deletions

View File

@ -1,12 +1,12 @@
package validator
import (
"fmt"
"npm/internal/entity/certificate"
"npm/internal/entity/host"
"npm/internal/entity/nginxtemplate"
"npm/internal/entity/upstream"
"github.com/rotisserie/eris"
)
// ValidateHost will check if associated objects exist and other checks
@ -17,32 +17,32 @@ func ValidateHost(h host.Model) error {
// This will not determine if the certificate is Ready to use,
// as this validation only cares that the row exists.
if _, cErr := certificate.GetByID(h.CertificateID); cErr != nil {
return fmt.Errorf("Certificate #%d does not exist", h.CertificateID)
return eris.Wrapf(cErr, "Certificate #%d does not exist", h.CertificateID)
}
}
if h.UpstreamID > 0 {
// Check upstream exists
if _, uErr := upstream.GetByID(h.UpstreamID); uErr != nil {
return fmt.Errorf("Upstream #%d does not exist", h.UpstreamID)
return eris.Wrapf(uErr, "Upstream #%d does not exist", h.UpstreamID)
}
}
// Ensure either UpstreamID is set or appropriate proxy host params are set
if h.UpstreamID > 0 && (h.ProxyHost != "" || h.ProxyPort > 0) {
return fmt.Errorf("Proxy Host or Port cannot be set when using an Upstream")
return eris.Errorf("Proxy Host or Port cannot be set when using an Upstream")
}
if h.UpstreamID == 0 && (h.ProxyHost == "" || h.ProxyPort < 1) {
return fmt.Errorf("Proxy Host and Port must be specified, unless using an Upstream")
return eris.Errorf("Proxy Host and Port must be specified, unless using an Upstream")
}
// Check the nginx template exists and has the same type.
nginxTemplate, tErr := nginxtemplate.GetByID(h.NginxTemplateID)
if tErr != nil {
return fmt.Errorf("Host Template #%d does not exist", h.NginxTemplateID)
return eris.Wrapf(tErr, "Host Template #%d does not exist", h.NginxTemplateID)
}
if nginxTemplate.Type != h.Type {
return fmt.Errorf("Host Template #%d is not valid for this host type", h.NginxTemplateID)
return eris.Errorf("Host Template #%d is not valid for this host type", h.NginxTemplateID)
}
return nil

View File

@ -1,11 +1,10 @@
package validator
import (
"errors"
"fmt"
"npm/internal/entity/nginxtemplate"
"npm/internal/entity/upstream"
"github.com/rotisserie/eris"
)
// ValidateUpstream will check if associated objects exist and other checks
@ -13,7 +12,7 @@ import (
func ValidateUpstream(u upstream.Model) error {
// Needs to have more than 1 server
if len(u.Servers) < 2 {
return errors.New("Upstreams require at least 2 servers")
return eris.New("Upstreams require at least 2 servers")
}
// Backup servers aren't permitted with hash balancing
@ -21,7 +20,7 @@ func ValidateUpstream(u upstream.Model) error {
// check all servers for a backup param
for _, server := range u.Servers {
if server.Backup {
return errors.New("Backup servers cannot be used with hash balancing")
return eris.New("Backup servers cannot be used with hash balancing")
}
}
}
@ -29,10 +28,10 @@ func ValidateUpstream(u upstream.Model) error {
// Check the nginx template exists and has the same type.
nginxTemplate, err := nginxtemplate.GetByID(u.NginxTemplateID)
if err != nil {
return fmt.Errorf("Nginx Template #%d does not exist", u.NginxTemplateID)
return eris.Errorf("Nginx Template #%d does not exist", u.NginxTemplateID)
}
if nginxTemplate.Type != "upstream" {
return fmt.Errorf("Host Template #%d is not valid for this upstream", u.NginxTemplateID)
return eris.Errorf("Host Template #%d is not valid for this upstream", u.NginxTemplateID)
}
return nil