mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-15 21:16:02 +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:
@@ -1,15 +1,12 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/netlink"
|
||||
)
|
||||
|
||||
var ErrRouteDefaultNotFound = errors.New("default route not found")
|
||||
|
||||
type DefaultRoute struct {
|
||||
NetInterface string
|
||||
Gateway netip.Addr
|
||||
@@ -60,7 +57,7 @@ func (r *Routing) DefaultRoutes() (defaultRoutes []DefaultRoute, err error) {
|
||||
}
|
||||
|
||||
if len(defaultRoutes) == 0 {
|
||||
return nil, fmt.Errorf("%w: in %d route(s)", ErrRouteDefaultNotFound, len(routes))
|
||||
return nil, fmt.Errorf("default route not found: in %d route(s)", len(routes))
|
||||
}
|
||||
|
||||
return defaultRoutes, nil
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrLinkDefaultNotFound = errors.New("default link not found")
|
||||
@@ -1,7 +1,6 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
@@ -14,8 +13,6 @@ func ipIsPrivate(ip netip.Addr) bool {
|
||||
ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast()
|
||||
}
|
||||
|
||||
var errInterfaceIPNotFound = errors.New("IP address not found for interface")
|
||||
|
||||
func ipMatchesFamily(ip netip.Addr, family uint8) bool {
|
||||
return (family == netlink.FamilyV4 && ip.Is4()) ||
|
||||
(family == netlink.FamilyV6 && ip.Is6())
|
||||
@@ -46,6 +43,6 @@ func (r *Routing) AssignedIP(interfaceName string, family uint8) (ip netip.Addr,
|
||||
|
||||
return ip, nil
|
||||
}
|
||||
return ip, fmt.Errorf("%w: interface %s in %d addresses",
|
||||
errInterfaceIPNotFound, interfaceName, len(addresses))
|
||||
return ip, fmt.Errorf("IP address not found for interface: interface %s in %d addresses",
|
||||
interfaceName, len(addresses))
|
||||
}
|
||||
|
||||
@@ -1,19 +1,12 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/netlink"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrLinkLocalNotFound = errors.New("local link not found")
|
||||
ErrSubnetDefaultNotFound = errors.New("default subnet not found")
|
||||
ErrSubnetLocalNotFound = errors.New("local subnet not found")
|
||||
)
|
||||
|
||||
type LocalNetwork struct {
|
||||
IPNet netip.Prefix
|
||||
InterfaceName string
|
||||
@@ -38,7 +31,7 @@ func (r *Routing) LocalNetworks() (localNetworks []LocalNetwork, err error) {
|
||||
}
|
||||
|
||||
if len(localLinks) == 0 {
|
||||
return localNetworks, fmt.Errorf("%w: in %d links", ErrLinkLocalNotFound, len(links))
|
||||
return localNetworks, fmt.Errorf("local link not found: in %d links", len(links))
|
||||
}
|
||||
|
||||
routes, err := r.netLinker.RouteList(netlink.FamilyAll)
|
||||
@@ -82,7 +75,7 @@ func (r *Routing) LocalNetworks() (localNetworks []LocalNetwork, err error) {
|
||||
}
|
||||
|
||||
if len(localNetworks) == 0 {
|
||||
return localNetworks, fmt.Errorf("%w: in %d routes", ErrSubnetLocalNotFound, len(routes))
|
||||
return localNetworks, fmt.Errorf("local subnet not found: in %d routes", len(routes))
|
||||
}
|
||||
|
||||
return localNetworks, nil
|
||||
|
||||
+4
-12
@@ -1,18 +1,12 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/netlink"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrVPNLocalGatewayIPNotFound = errors.New("VPN local gateway IP address not found")
|
||||
ErrVPNLocalGatewayIPv6NotSupported = errors.New("VPN local gateway IPv6 address not supported")
|
||||
)
|
||||
|
||||
func (r *Routing) VPNLocalGatewayIP(vpnIntf string) (ip netip.Addr, err error) {
|
||||
vpnLink, err := r.netLinker.LinkByName(vpnIntf)
|
||||
if err != nil {
|
||||
@@ -36,7 +30,7 @@ func (r *Routing) VPNLocalGatewayIP(vpnIntf string) (ip netip.Addr, err error) {
|
||||
route.Dst.Addr().Compare(route.Src.Addr()) == 0 &&
|
||||
route.Table == tableLocal: // Wireguard
|
||||
if route.Src.Addr().Is6() {
|
||||
return netip.Addr{}, fmt.Errorf("%w: %s", ErrVPNLocalGatewayIPv6NotSupported, route.Src)
|
||||
return netip.Addr{}, fmt.Errorf("VPN local gateway IPv6 address not supported: %s", route.Src)
|
||||
}
|
||||
bytes := route.Src.Addr().As4()
|
||||
// force last byte to 1 to get the VPN gateway IP
|
||||
@@ -45,11 +39,9 @@ func (r *Routing) VPNLocalGatewayIP(vpnIntf string) (ip netip.Addr, err error) {
|
||||
return netip.AddrFrom4(bytes), nil
|
||||
}
|
||||
}
|
||||
return ip, fmt.Errorf("%w: in %d routes", ErrVPNLocalGatewayIPNotFound, len(routes))
|
||||
return ip, fmt.Errorf("VPN local gateway IP address not found: in %d routes", len(routes))
|
||||
}
|
||||
|
||||
var ErrVPNRouteNotFound = errors.New("VPN route not found")
|
||||
|
||||
// VPNRoutes returns the routes that are using the VPN interface, excluding local routes
|
||||
// and link-local multicast and unicast routes.
|
||||
func (r *Routing) VPNRoutes(vpnIntf string) (routes []netlink.Route, err error) {
|
||||
@@ -80,8 +72,8 @@ func (r *Routing) VPNRoutes(vpnIntf string) (routes []netlink.Route, err error)
|
||||
}
|
||||
|
||||
if len(routes) == 0 {
|
||||
return nil, fmt.Errorf("%w: for interface %s in %d routes",
|
||||
ErrVPNRouteNotFound, vpnIntf, len(allRoutes))
|
||||
return nil, fmt.Errorf("VPN route not found: for interface %s in %d routes",
|
||||
vpnIntf, len(allRoutes))
|
||||
}
|
||||
|
||||
return routes, nil
|
||||
|
||||
Reference in New Issue
Block a user