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:
Quentin McGaw
2026-05-02 00:50:16 +00:00
parent 9b6f048fe8
commit 4a78989d9d
172 changed files with 666 additions and 1433 deletions
+3 -20
View File
@@ -84,25 +84,21 @@ func Test_Settings_Check(t *testing.T) {
testCases := map[string]struct {
settings Settings
errWrapped error
errMessage string
}{
"empty settings": {
errWrapped: ErrInterfaceNameInvalid,
errMessage: "invalid interface name: ",
},
"bad interface name": {
settings: Settings{
InterfaceName: "$H1T",
},
errWrapped: ErrInterfaceNameInvalid,
errMessage: "invalid interface name: $H1T",
},
"empty private key": {
settings: Settings{
InterfaceName: "wg0",
},
errWrapped: ErrPrivateKeyMissing,
errMessage: "private key is missing",
},
"bad private key": {
@@ -110,7 +106,6 @@ func Test_Settings_Check(t *testing.T) {
InterfaceName: "wg0",
PrivateKey: "bad key",
},
errWrapped: ErrPrivateKeyInvalid,
errMessage: "cannot parse private key",
},
"empty public key": {
@@ -118,7 +113,6 @@ func Test_Settings_Check(t *testing.T) {
InterfaceName: "wg0",
PrivateKey: validKey1,
},
errWrapped: ErrPublicKeyMissing,
errMessage: "public key is missing",
},
"bad public key": {
@@ -127,7 +121,6 @@ func Test_Settings_Check(t *testing.T) {
PrivateKey: validKey1,
PublicKey: "bad key",
},
errWrapped: ErrPublicKeyInvalid,
errMessage: "cannot parse public key: bad key",
},
"bad preshared key": {
@@ -137,7 +130,6 @@ func Test_Settings_Check(t *testing.T) {
PublicKey: validKey2,
PreSharedKey: "bad key",
},
errWrapped: ErrPreSharedKeyInvalid,
errMessage: "cannot parse pre-shared key",
},
"invalid endpoint address": {
@@ -146,7 +138,6 @@ func Test_Settings_Check(t *testing.T) {
PrivateKey: validKey1,
PublicKey: validKey2,
},
errWrapped: ErrEndpointAddrMissing,
errMessage: "endpoint address is missing",
},
"zero endpoint port": {
@@ -156,7 +147,6 @@ func Test_Settings_Check(t *testing.T) {
PublicKey: validKey2,
Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 0),
},
errWrapped: ErrEndpointPortMissing,
errMessage: "endpoint port is missing",
},
"no address": {
@@ -166,7 +156,6 @@ func Test_Settings_Check(t *testing.T) {
PublicKey: validKey2,
Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820),
},
errWrapped: ErrAddressMissing,
errMessage: "interface address is missing",
},
"invalid address": {
@@ -177,7 +166,6 @@ func Test_Settings_Check(t *testing.T) {
Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820),
Addresses: []netip.Prefix{{}},
},
errWrapped: ErrAddressNotValid,
errMessage: "interface address is not valid: for address 1 of 1",
},
@@ -191,7 +179,6 @@ func Test_Settings_Check(t *testing.T) {
netip.PrefixFrom(netip.AddrFrom4([4]byte{5, 6, 7, 8}), 24),
},
},
errWrapped: ErrAllowedIPsMissing,
errMessage: "allowed IPs are missing",
},
"invalid allowed IP": {
@@ -205,7 +192,6 @@ func Test_Settings_Check(t *testing.T) {
},
AllowedIPs: []netip.Prefix{{}},
},
errWrapped: ErrAllowedIPNotValid,
errMessage: "allowed IP is not valid: for allowed IP 1 of 1",
},
"ipv6 allowed IP": {
@@ -222,7 +208,6 @@ func Test_Settings_Check(t *testing.T) {
},
IPv6: ptrTo(false),
},
errWrapped: ErrAllowedIPv6NotSupported,
errMessage: "allowed IPv6 address not supported: for allowed IP ::/0",
},
"zero firewall mark": {
@@ -236,7 +221,6 @@ func Test_Settings_Check(t *testing.T) {
netip.PrefixFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 24),
},
},
errWrapped: ErrFirewallMarkMissing,
errMessage: "firewall mark is missing",
},
"missing_MTU": {
@@ -251,7 +235,6 @@ func Test_Settings_Check(t *testing.T) {
},
FirewallMark: 999,
},
errWrapped: ErrMTUMissing,
errMessage: "MTU is missing",
},
"invalid implementation": {
@@ -268,7 +251,6 @@ func Test_Settings_Check(t *testing.T) {
MTU: 1420,
Implementation: "x",
},
errWrapped: ErrImplementationInvalid,
errMessage: "invalid implementation: x",
},
"all valid": {
@@ -297,9 +279,10 @@ func Test_Settings_Check(t *testing.T) {
err := testCase.settings.Check()
assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
if testCase.errMessage != "" {
assert.EqualError(t, err, testCase.errMessage)
} else {
assert.NoError(t, err)
}
})
}