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) { switch len(providers) {
case 0: case 0:
return ErrProviderUnspecified return fmt.Errorf("%w", ErrProviderUnspecified)
case 1: case 1:
default: default:
return fmt.Errorf("%w: %d specified: %s", 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 { if !endUserMode && !maintainerMode {
return ErrModeUnspecified return fmt.Errorf("%w", ErrModeUnspecified)
} }
if updateAll { if updateAll {
options.Providers = providers.All() options.Providers = providers.All()
} else { } else {
if csvProviders == "" { if csvProviders == "" {
return ErrNoProviderSpecified return fmt.Errorf("%w", ErrNoProviderSpecified)
} }
options.Providers = strings.Split(csvProviders, ",") options.Providers = strings.Split(csvProviders, ",")
} }

View File

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

View File

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

View File

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

View File

@@ -52,7 +52,7 @@ func (w Wireguard) validate(vpnProvider string, ipv6Supported bool) (err error)
// Validate PrivateKey // Validate PrivateKey
if *w.PrivateKey == "" { if *w.PrivateKey == "" {
return ErrWireguardPrivateKeyNotSet return fmt.Errorf("%w", ErrWireguardPrivateKeyNotSet)
} }
_, err = wgtypes.ParseKey(*w.PrivateKey) _, err = wgtypes.ParseKey(*w.PrivateKey)
if err != nil { if err != nil {
@@ -75,7 +75,7 @@ func (w Wireguard) validate(vpnProvider string, ipv6Supported bool) (err error)
// Validate Addresses // Validate Addresses
if len(w.Addresses) == 0 { if len(w.Addresses) == 0 {
return ErrWireguardInterfaceAddressNotSet return fmt.Errorf("%w", ErrWireguardInterfaceAddressNotSet)
} }
for i, ipNet := range w.Addresses { for i, ipNet := range w.Addresses {
if ipNet.IP == nil || ipNet.Mask == nil { 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 // endpoint IP addresses are baked in
case providers.Custom: case providers.Custom:
if len(w.EndpointIP) == 0 { if len(w.EndpointIP) == 0 {
return ErrWireguardEndpointIPNotSet return fmt.Errorf("%w", ErrWireguardEndpointIPNotSet)
} }
default: // Providers not supporting Wireguard default: // Providers not supporting Wireguard
} }
@@ -51,12 +51,12 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) {
// EndpointPort is required // EndpointPort is required
case providers.Custom: case providers.Custom:
if *w.EndpointPort == 0 { if *w.EndpointPort == 0 {
return ErrWireguardEndpointPortNotSet return fmt.Errorf("%w", ErrWireguardEndpointPortNotSet)
} }
// EndpointPort cannot be set // EndpointPort cannot be set
case providers.Surfshark: case providers.Surfshark:
if *w.EndpointPort != 0 { if *w.EndpointPort != 0 {
return ErrWireguardEndpointPortSet return fmt.Errorf("%w", ErrWireguardEndpointPortSet)
} }
case providers.Airvpn, providers.Ivpn, providers.Mullvad, providers.Windscribe: case providers.Airvpn, providers.Ivpn, providers.Mullvad, providers.Windscribe:
// EndpointPort is optional and can be 0 // EndpointPort is optional and can be 0
@@ -92,7 +92,7 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) {
// public keys are baked in // public keys are baked in
case providers.Custom: case providers.Custom:
if w.PublicKey == "" { if w.PublicKey == "" {
return ErrWireguardPublicKeyNotSet return fmt.Errorf("%w", ErrWireguardPublicKeyNotSet)
} }
default: // Providers not supporting Wireguard default: // Providers not supporting Wireguard
} }

View File

@@ -2,6 +2,7 @@ package httpproxy
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@@ -65,5 +66,5 @@ var hopHeaders = [...]string{ //nolint:gochecknoglobals
// Do not follow redirect, but directly return the redirect response. // Do not follow redirect, but directly return the redirect response.
func returnRedirect(*http.Request, []*http.Request) error { 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 { if s.Handler == nil {
return ErrHandlerIsNotSet return fmt.Errorf("%w", ErrHandlerIsNotSet)
} }
if s.Logger == nil { if s.Logger == nil {
return ErrLoggerIsNotSet return fmt.Errorf("%w", ErrLoggerIsNotSet)
} }
const minReadTimeout = time.Millisecond const minReadTimeout = time.Millisecond

View File

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

View File

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

View File

@@ -25,7 +25,7 @@ func (hts hostToServer) add(data serverData) (err error) {
} }
if data.IPv4 == "" && data.IPv6 == "" { if data.IPv4 == "" && data.IPv6 == "" {
return ErrNoIP return fmt.Errorf("%w", ErrNoIP)
} }
server, ok := hts[data.Hostname] 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) sys, ok := info.Sys().(*syscall.Stat_t)
if !ok { if !ok {
return ErrTUNInfo return fmt.Errorf("%w", ErrTUNInfo)
} }
const expectedRdev = 2760 // corresponds to major 10 and minor 200 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 == "" { if s.PrivateKey == "" {
return ErrPrivateKeyMissing return fmt.Errorf("%w", ErrPrivateKeyMissing)
} else if _, err := wgtypes.ParseKey(s.PrivateKey); err != nil { } else if _, err := wgtypes.ParseKey(s.PrivateKey); err != nil {
return ErrPrivateKeyInvalid return fmt.Errorf("%w", ErrPrivateKeyInvalid)
} }
if s.PublicKey == "" { if s.PublicKey == "" {
return ErrPublicKeyMissing return fmt.Errorf("%w", ErrPublicKeyMissing)
} else if _, err := wgtypes.ParseKey(s.PublicKey); err != nil { } else if _, err := wgtypes.ParseKey(s.PublicKey); err != nil {
return fmt.Errorf("%w: %s", ErrPublicKeyInvalid, s.PublicKey) return fmt.Errorf("%w: %s", ErrPublicKeyInvalid, s.PublicKey)
} }
if s.PreSharedKey != "" { if s.PreSharedKey != "" {
if _, err := wgtypes.ParseKey(s.PreSharedKey); err != nil { if _, err := wgtypes.ParseKey(s.PreSharedKey); err != nil {
return ErrPreSharedKeyInvalid return fmt.Errorf("%w", ErrPreSharedKeyInvalid)
} }
} }
switch { switch {
case s.Endpoint == nil: case s.Endpoint == nil:
return ErrEndpointMissing return fmt.Errorf("%w", ErrEndpointMissing)
case len(s.Endpoint.IP) == 0: case len(s.Endpoint.IP) == 0:
return ErrEndpointIPMissing return fmt.Errorf("%w", ErrEndpointIPMissing)
case s.Endpoint.Port == 0: case s.Endpoint.Port == 0:
return ErrEndpointPortMissing return fmt.Errorf("%w", ErrEndpointPortMissing)
} }
if len(s.Addresses) == 0 { if len(s.Addresses) == 0 {
return ErrAddressMissing return fmt.Errorf("%w", ErrAddressMissing)
} }
for i, addr := range s.Addresses { for i, addr := range s.Addresses {
switch { switch {
@@ -136,7 +136,7 @@ func (s *Settings) Check() (err error) {
} }
if s.FirewallMark == 0 { if s.FirewallMark == 0 {
return ErrFirewallMarkMissing return fmt.Errorf("%w", ErrFirewallMarkMissing)
} }
switch s.Implementation { switch s.Implementation {