mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-06 20:10:11 +02:00
chore: do not use sentinel errors when unneeded
- main reason being it's a burden to always define sentinel errors at global scope, wrap them with `%w` instead of using a string directly - only use sentinel errors when it has to be checked using `errors.Is` - replace all usage of these sentinel errors in `fmt.Errorf` with direct strings that were in the sentinel error - exclude the sentinel error definition requirement from .golangci.yml - update unit tests to use ContainersError instead of ErrorIs so it stays as a "not a change detector test" without requiring a sentinel error
This commit is contained in:
@@ -177,11 +177,9 @@ func (l *Loop) GetPortsForwarded() (ports []uint16) {
|
||||
return l.service.GetPortsForwarded()
|
||||
}
|
||||
|
||||
var ErrServiceNotStarted = errors.New("port forwarding service not started")
|
||||
|
||||
func (l *Loop) SetPortsForwarded(ports []uint16) (err error) {
|
||||
if l.service == nil {
|
||||
return fmt.Errorf("%w", ErrServiceNotStarted)
|
||||
return errors.New("port forwarding service not started")
|
||||
}
|
||||
|
||||
return l.service.SetPortsForwarded(l.runCtx, ports)
|
||||
|
||||
@@ -55,23 +55,10 @@ func (s *Settings) OverrideWith(update Settings) {
|
||||
s.Password = gosettings.OverrideWithComparable(s.Password, update.Password)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrPortForwarderNotSet = errors.New("port forwarder not set")
|
||||
ErrServerNameNotSet = errors.New("server name not set")
|
||||
ErrUsernameNotSet = errors.New("username not set")
|
||||
ErrPasswordNotSet = errors.New("password not set")
|
||||
ErrFilepathNotSet = errors.New("file path not set")
|
||||
ErrInterfaceNotSet = errors.New("interface not set")
|
||||
ErrPortsCountZero = errors.New("ports count cannot be zero")
|
||||
ErrPortsCountTooHigh = errors.New("ports count too high")
|
||||
ErrListeningPortsLen = errors.New("listening ports length must be equal to ports count")
|
||||
ErrListeningPortZero = errors.New("listening port cannot be 0")
|
||||
)
|
||||
|
||||
func (s *Settings) Validate(forStartup bool) (err error) {
|
||||
// Minimal validation
|
||||
if s.Filepath == "" {
|
||||
return fmt.Errorf("%w", ErrFilepathNotSet)
|
||||
return errors.New("file path not set")
|
||||
}
|
||||
|
||||
if !forStartup {
|
||||
@@ -83,41 +70,42 @@ func (s *Settings) Validate(forStartup bool) (err error) {
|
||||
// Startup validation requires additional fields set.
|
||||
switch {
|
||||
case s.PortForwarder == nil:
|
||||
return fmt.Errorf("%w", ErrPortForwarderNotSet)
|
||||
return errors.New("port forwarder not set")
|
||||
case s.Interface == "":
|
||||
return fmt.Errorf("%w", ErrInterfaceNotSet)
|
||||
return errors.New("interface not set")
|
||||
case s.PortsCount == 0:
|
||||
return fmt.Errorf("%w", ErrPortsCountZero)
|
||||
return errors.New("ports count cannot be zero")
|
||||
}
|
||||
|
||||
switch s.PortForwarder.Name() {
|
||||
case providers.PrivateInternetAccess:
|
||||
switch {
|
||||
case s.ServerName == "":
|
||||
return fmt.Errorf("%w", ErrServerNameNotSet)
|
||||
return errors.New("server name not set")
|
||||
case s.Username == "":
|
||||
return fmt.Errorf("%w", ErrUsernameNotSet)
|
||||
return errors.New("username not set")
|
||||
case s.Password == "":
|
||||
return fmt.Errorf("%w", ErrPasswordNotSet)
|
||||
return errors.New("password not set")
|
||||
}
|
||||
case providers.Protonvpn:
|
||||
const maxPortsCount = 4
|
||||
if s.PortsCount > maxPortsCount {
|
||||
return fmt.Errorf("%w: %d > %d", ErrPortsCountTooHigh, s.PortsCount, maxPortsCount)
|
||||
return fmt.Errorf("ports count too high: %d > %d", s.PortsCount, maxPortsCount)
|
||||
}
|
||||
default:
|
||||
const maxPortsCount = 1
|
||||
if s.PortsCount > maxPortsCount {
|
||||
return fmt.Errorf("%w: %d > %d", ErrPortsCountTooHigh, s.PortsCount, maxPortsCount)
|
||||
return fmt.Errorf("ports count too high: %d > %d", s.PortsCount, maxPortsCount)
|
||||
}
|
||||
}
|
||||
|
||||
if !slices.Equal(s.ListeningPorts, []uint16{0}) {
|
||||
switch {
|
||||
case len(s.ListeningPorts) != int(s.PortsCount):
|
||||
return fmt.Errorf("%w: %d != %d", ErrListeningPortsLen, len(s.ListeningPorts), s.PortsCount)
|
||||
return fmt.Errorf("listening ports length must be equal to ports count: %d != %d",
|
||||
len(s.ListeningPorts), s.PortsCount)
|
||||
case slices.Contains(s.ListeningPorts, 0):
|
||||
return fmt.Errorf("%w: in %v", ErrListeningPortZero, s.ListeningPorts)
|
||||
return fmt.Errorf("listening port cannot be 0: in %v", s.ListeningPorts)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user