chore(all): wrap all sentinel errors

- Force to use `errors.Is` instead of `==` to compare errors
This commit is contained in:
Quentin McGaw
2023-04-12 10:18:16 +00:00
parent fa7fd5f076
commit 219d1f371c
14 changed files with 41 additions and 39 deletions

View File

@@ -61,7 +61,7 @@ func (c *CLI) FormatServers(args []string) error {
}
switch len(providers) {
case 0:
return ErrProviderUnspecified
return fmt.Errorf("%w", ErrProviderUnspecified)
case 1:
default:
return fmt.Errorf("%w: %d specified: %s",

View File

@@ -51,14 +51,14 @@ func (c *CLI) Update(ctx context.Context, args []string, logger UpdaterLogger) e
}
if !endUserMode && !maintainerMode {
return ErrModeUnspecified
return fmt.Errorf("%w", ErrModeUnspecified)
}
if updateAll {
options.Providers = providers.All()
} else {
if csvProviders == "" {
return ErrNoProviderSpecified
return fmt.Errorf("%w", ErrNoProviderSpecified)
}
options.Providers = strings.Split(csvProviders, ",")
}

View File

@@ -22,7 +22,7 @@ var (
func AreAllOneOf(values, choices []string) (err error) {
if len(values) > 0 && len(choices) == 0 {
return ErrNoChoice
return fmt.Errorf("%w", ErrNoChoice)
}
set := make(map[string]struct{}, len(choices))

View File

@@ -100,14 +100,14 @@ func (o OpenVPN) validate(vpnProvider string) (err error) {
vpnProvider != providers.VPNSecure
if isUserRequired && *o.User == "" {
return ErrOpenVPNUserIsEmpty
return fmt.Errorf("%w", ErrOpenVPNUserIsEmpty)
}
passwordRequired := isUserRequired &&
(vpnProvider != providers.Ivpn || !ivpnAccountID.MatchString(*o.User))
if passwordRequired && *o.Password == "" {
return ErrOpenVPNPasswordIsEmpty
return fmt.Errorf("%w", ErrOpenVPNPasswordIsEmpty)
}
err = validateOpenVPNConfigFilepath(isCustom, *o.ConfFile)
@@ -160,7 +160,7 @@ func validateOpenVPNConfigFilepath(isCustom bool,
}
if confFile == "" {
return ErrFilepathMissing
return fmt.Errorf("%w", ErrFilepathMissing)
}
err = helpers.FileExists(confFile)
@@ -186,7 +186,7 @@ func validateOpenVPNClientCertificate(vpnProvider,
providers.VPNSecure,
providers.VPNUnlimited:
if clientCert == "" {
return ErrMissingValue
return fmt.Errorf("%w", ErrMissingValue)
}
}
@@ -209,7 +209,7 @@ func validateOpenVPNClientKey(vpnProvider, clientKey string) (err error) {
providers.VPNUnlimited,
providers.Wevpn:
if clientKey == "" {
return ErrMissingValue
return fmt.Errorf("%w", ErrMissingValue)
}
}
@@ -227,7 +227,7 @@ func validateOpenVPNClientKey(vpnProvider, clientKey string) (err error) {
func validateOpenVPNEncryptedKey(vpnProvider,
encryptedPrivateKey string) (err error) {
if vpnProvider == providers.VPNSecure && encryptedPrivateKey == "" {
return ErrMissingValue
return fmt.Errorf("%w", ErrMissingValue)
}
if encryptedPrivateKey == "" {

View File

@@ -118,7 +118,7 @@ func (ss *ServerSelection) validate(vpnServiceProvider string,
}
if *ss.FreeOnly && *ss.PremiumOnly {
return ErrFreePremiumBothSet
return fmt.Errorf("%w", ErrFreePremiumBothSet)
}
if *ss.StreamOnly &&

View File

@@ -52,7 +52,7 @@ func (w Wireguard) validate(vpnProvider string, ipv6Supported bool) (err error)
// Validate PrivateKey
if *w.PrivateKey == "" {
return ErrWireguardPrivateKeyNotSet
return fmt.Errorf("%w", ErrWireguardPrivateKeyNotSet)
}
_, err = wgtypes.ParseKey(*w.PrivateKey)
if err != nil {
@@ -75,7 +75,7 @@ func (w Wireguard) validate(vpnProvider string, ipv6Supported bool) (err error)
// Validate Addresses
if len(w.Addresses) == 0 {
return ErrWireguardInterfaceAddressNotSet
return fmt.Errorf("%w", ErrWireguardInterfaceAddressNotSet)
}
for i, ipNet := range w.Addresses {
if ipNet.IP == nil || ipNet.Mask == nil {

View File

@@ -41,7 +41,7 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) {
// endpoint IP addresses are baked in
case providers.Custom:
if len(w.EndpointIP) == 0 {
return ErrWireguardEndpointIPNotSet
return fmt.Errorf("%w", ErrWireguardEndpointIPNotSet)
}
default: // Providers not supporting Wireguard
}
@@ -51,12 +51,12 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) {
// EndpointPort is required
case providers.Custom:
if *w.EndpointPort == 0 {
return ErrWireguardEndpointPortNotSet
return fmt.Errorf("%w", ErrWireguardEndpointPortNotSet)
}
// EndpointPort cannot be set
case providers.Surfshark:
if *w.EndpointPort != 0 {
return ErrWireguardEndpointPortSet
return fmt.Errorf("%w", ErrWireguardEndpointPortSet)
}
case providers.Airvpn, providers.Ivpn, providers.Mullvad, providers.Windscribe:
// EndpointPort is optional and can be 0
@@ -92,7 +92,7 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) {
// public keys are baked in
case providers.Custom:
if w.PublicKey == "" {
return ErrWireguardPublicKeyNotSet
return fmt.Errorf("%w", ErrWireguardPublicKeyNotSet)
}
default: // Providers not supporting Wireguard
}

View File

@@ -2,6 +2,7 @@ package httpproxy
import (
"context"
"fmt"
"net/http"
"sync"
"time"
@@ -65,5 +66,5 @@ var hopHeaders = [...]string{ //nolint:gochecknoglobals
// Do not follow redirect, but directly return the redirect response.
func returnRedirect(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
return fmt.Errorf("%w", http.ErrUseLastResponse)
}

View File

@@ -91,11 +91,11 @@ func (s Settings) Validate() (err error) {
}
if s.Handler == nil {
return ErrHandlerIsNotSet
return fmt.Errorf("%w", ErrHandlerIsNotSet)
}
if s.Logger == nil {
return ErrLoggerIsNotSet
return fmt.Errorf("%w", ErrLoggerIsNotSet)
}
const minReadTimeout = time.Millisecond

View File

@@ -47,17 +47,17 @@ var (
func (s *Server) HasMinimumInformation() (err error) {
switch {
case s.VPN == "":
return ErrVPNFieldEmpty
return fmt.Errorf("%w", ErrVPNFieldEmpty)
case s.Hostname == "":
return ErrHostnameFieldEmpty
return fmt.Errorf("%w", ErrHostnameFieldEmpty)
case len(s.IPs) == 0:
return ErrIPsFieldEmpty
return fmt.Errorf("%w", ErrIPsFieldEmpty)
case s.VPN == vpn.Wireguard && (s.TCP || s.UDP):
return ErrNetworkProtocolSet
return fmt.Errorf("%w", ErrNetworkProtocolSet)
case s.VPN == vpn.OpenVPN && !s.TCP && !s.UDP:
return ErrNoNetworkProtocol
return fmt.Errorf("%w", ErrNoNetworkProtocol)
case s.VPN == vpn.Wireguard && s.WgPubKey == "":
return ErrWireguardPublicKeyEmpty
return fmt.Errorf("%w", ErrWireguardPublicKeyEmpty)
default:
return nil
}

View File

@@ -2,6 +2,7 @@ package pprof
import (
"errors"
"fmt"
"time"
"github.com/qdm12/gluetun/internal/configuration/settings/helpers"
@@ -63,11 +64,11 @@ var (
func (s Settings) Validate() (err error) {
if *s.BlockProfileRate < 0 {
return ErrBlockProfileRateNegative
return fmt.Errorf("%w", ErrBlockProfileRateNegative)
}
if *s.MutexProfileRate < 0 {
return ErrMutexProfileRateNegative
return fmt.Errorf("%w", ErrMutexProfileRateNegative)
}
return s.HTTPServer.Validate()

View File

@@ -25,7 +25,7 @@ func (hts hostToServer) add(data serverData) (err error) {
}
if data.IPv4 == "" && data.IPv6 == "" {
return ErrNoIP
return fmt.Errorf("%w", ErrNoIP)
}
server, ok := hts[data.Hostname]

View File

@@ -27,7 +27,7 @@ func (t *Tun) Check(path string) error {
sys, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return ErrTUNInfo
return fmt.Errorf("%w", ErrTUNInfo)
}
const expectedRdev = 2760 // corresponds to major 10 and minor 200

View File

@@ -92,34 +92,34 @@ func (s *Settings) Check() (err error) {
}
if s.PrivateKey == "" {
return ErrPrivateKeyMissing
return fmt.Errorf("%w", ErrPrivateKeyMissing)
} else if _, err := wgtypes.ParseKey(s.PrivateKey); err != nil {
return ErrPrivateKeyInvalid
return fmt.Errorf("%w", ErrPrivateKeyInvalid)
}
if s.PublicKey == "" {
return ErrPublicKeyMissing
return fmt.Errorf("%w", ErrPublicKeyMissing)
} else if _, err := wgtypes.ParseKey(s.PublicKey); err != nil {
return fmt.Errorf("%w: %s", ErrPublicKeyInvalid, s.PublicKey)
}
if s.PreSharedKey != "" {
if _, err := wgtypes.ParseKey(s.PreSharedKey); err != nil {
return ErrPreSharedKeyInvalid
return fmt.Errorf("%w", ErrPreSharedKeyInvalid)
}
}
switch {
case s.Endpoint == nil:
return ErrEndpointMissing
return fmt.Errorf("%w", ErrEndpointMissing)
case len(s.Endpoint.IP) == 0:
return ErrEndpointIPMissing
return fmt.Errorf("%w", ErrEndpointIPMissing)
case s.Endpoint.Port == 0:
return ErrEndpointPortMissing
return fmt.Errorf("%w", ErrEndpointPortMissing)
}
if len(s.Addresses) == 0 {
return ErrAddressMissing
return fmt.Errorf("%w", ErrAddressMissing)
}
for i, addr := range s.Addresses {
switch {
@@ -136,7 +136,7 @@ func (s *Settings) Check() (err error) {
}
if s.FirewallMark == 0 {
return ErrFirewallMarkMissing
return fmt.Errorf("%w", ErrFirewallMarkMissing)
}
switch s.Implementation {