mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-17 06:00:15 +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:
@@ -19,8 +19,6 @@ import (
|
||||
func Test_GetConnection(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
errTest := errors.New("test error")
|
||||
|
||||
testCases := map[string]struct {
|
||||
provider string
|
||||
filteredServers []models.Server
|
||||
@@ -30,12 +28,10 @@ func Test_GetConnection(t *testing.T) {
|
||||
ipv6Supported bool
|
||||
randSource rand.Source
|
||||
connection models.Connection
|
||||
errWrapped error
|
||||
errMessage string
|
||||
}{
|
||||
"storage filter error": {
|
||||
filterError: errTest,
|
||||
errWrapped: errTest,
|
||||
filterError: errors.New("test error"),
|
||||
errMessage: "filtering servers: test error",
|
||||
},
|
||||
"server without IPs": {
|
||||
@@ -50,7 +46,6 @@ func Test_GetConnection(t *testing.T) {
|
||||
OpenVPNUDPPort: 1,
|
||||
WireguardPort: 1,
|
||||
},
|
||||
errWrapped: ErrNoConnectionToPickFrom,
|
||||
errMessage: "no connection to pick from",
|
||||
},
|
||||
"OpenVPN server with hostname": {
|
||||
@@ -199,9 +194,10 @@ func Test_GetConnection(t *testing.T) {
|
||||
testCase.randSource)
|
||||
|
||||
assert.Equal(t, testCase.connection, connection)
|
||||
assert.ErrorIs(t, err, testCase.errWrapped)
|
||||
if testCase.errWrapped != nil {
|
||||
if testCase.errMessage != "" {
|
||||
assert.EqualError(t, err, testCase.errMessage)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
@@ -18,10 +17,8 @@ func NewNoFetcher(providerName string) *NoFetcher {
|
||||
}
|
||||
}
|
||||
|
||||
var ErrFetcherNotSupported = errors.New("fetching of servers is not supported")
|
||||
|
||||
func (n *NoFetcher) FetchServers(context.Context, int) (
|
||||
servers []models.Server, err error,
|
||||
) {
|
||||
return nil, fmt.Errorf("%w: for %s", ErrFetcherNotSupported, n.providerName)
|
||||
return nil, fmt.Errorf("fetching of servers is not supported for %s", n.providerName)
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var ErrNoConnectionToPickFrom = errors.New("no connection to pick from")
|
||||
|
||||
// pickConnection picks a connection from a pool of connections.
|
||||
// If the VPN protocol is Wireguard and the target IP is set,
|
||||
// it finds the connection corresponding to this target IP.
|
||||
@@ -23,7 +21,7 @@ func pickConnection(connections []models.Connection,
|
||||
connection models.Connection, err error,
|
||||
) {
|
||||
if len(connections) == 0 {
|
||||
return connection, ErrNoConnectionToPickFrom
|
||||
return connection, errors.New("no connection to pick from")
|
||||
}
|
||||
|
||||
var targetIP netip.Addr
|
||||
@@ -56,8 +54,6 @@ func pickRandomConnection(connections []models.Connection,
|
||||
return connections[rand.New(source).Intn(len(connections))] //nolint:gosec
|
||||
}
|
||||
|
||||
var errTargetIPNotFound = errors.New("target IP address not found")
|
||||
|
||||
func getTargetIPConnection(connections []models.Connection,
|
||||
targetIP netip.Addr,
|
||||
) (connection models.Connection, err error) {
|
||||
@@ -66,6 +62,6 @@ func getTargetIPConnection(connections []models.Connection,
|
||||
return connection, nil
|
||||
}
|
||||
}
|
||||
return connection, fmt.Errorf("%w: in %d filtered connections",
|
||||
errTargetIPNotFound, len(connections))
|
||||
return connection, fmt.Errorf("target IP address not found: in %d filtered connections",
|
||||
len(connections))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user