mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-07 04:20:12 +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:
@@ -11,8 +11,6 @@ import (
|
||||
"github.com/qdm12/gluetun/internal/models"
|
||||
)
|
||||
|
||||
var errRemoteLineNotFound = errors.New("remote line not found")
|
||||
|
||||
func extractDataFromLines(lines []string) (
|
||||
connection models.Connection, err error,
|
||||
) {
|
||||
@@ -35,7 +33,7 @@ func extractDataFromLines(lines []string) (
|
||||
}
|
||||
|
||||
if !connection.IP.IsValid() {
|
||||
return connection, errRemoteLineNotFound
|
||||
return connection, errors.New("remote line not found")
|
||||
}
|
||||
|
||||
if connection.Protocol == "" {
|
||||
@@ -81,19 +79,15 @@ func extractDataFromLine(line string) (
|
||||
return ip, 0, "", nil
|
||||
}
|
||||
|
||||
var errProtoLineFieldsCount = errors.New("proto line has not 2 fields as expected")
|
||||
|
||||
func extractProto(line string) (protocol string, err error) {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 { //nolint:mnd
|
||||
return "", fmt.Errorf("%w: %s", errProtoLineFieldsCount, line)
|
||||
return "", fmt.Errorf("proto line has not 2 fields as expected: %s", line)
|
||||
}
|
||||
|
||||
return parseProto(fields[1])
|
||||
}
|
||||
|
||||
var errProtocolNotSupported = errors.New("network protocol not supported")
|
||||
|
||||
func parseProto(field string) (protocol string, err error) {
|
||||
switch field {
|
||||
case "tcp", "tcp4", "tcp6", "tcp-client":
|
||||
@@ -106,16 +100,10 @@ func parseProto(field string) (protocol string, err error) {
|
||||
// determined by the remote IP address version.
|
||||
return constants.UDP, nil
|
||||
default:
|
||||
return "", fmt.Errorf("%w: %s", errProtocolNotSupported, field)
|
||||
return "", fmt.Errorf("network protocol not supported: %s", field)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
errRemoteLineFieldsCount = errors.New("remote line has not 2 fields as expected")
|
||||
errHostNotIP = errors.New("host is not an IP address")
|
||||
errPortNotValid = errors.New("port is not valid")
|
||||
)
|
||||
|
||||
func extractRemote(line string) (ip netip.Addr, port uint16,
|
||||
protocol string, err error,
|
||||
) {
|
||||
@@ -123,13 +111,13 @@ func extractRemote(line string) (ip netip.Addr, port uint16,
|
||||
n := len(fields)
|
||||
|
||||
if n < 2 || n > 4 {
|
||||
return netip.Addr{}, 0, "", fmt.Errorf("%w: %s", errRemoteLineFieldsCount, line)
|
||||
return netip.Addr{}, 0, "", fmt.Errorf("remote line has not 2 fields as expected: %s", line)
|
||||
}
|
||||
|
||||
host := fields[1]
|
||||
ip, err = netip.ParseAddr(host)
|
||||
if err != nil {
|
||||
return netip.Addr{}, 0, "", fmt.Errorf("%w: %s", errHostNotIP, host)
|
||||
return netip.Addr{}, 0, "", fmt.Errorf("host is not an IP address: %s", host)
|
||||
// TODO resolve hostname once there is an option to allow it through
|
||||
// the firewall before the VPN is up.
|
||||
}
|
||||
@@ -137,9 +125,9 @@ func extractRemote(line string) (ip netip.Addr, port uint16,
|
||||
if n > 2 { //nolint:mnd
|
||||
portInt, err := strconv.Atoi(fields[2])
|
||||
if err != nil {
|
||||
return netip.Addr{}, 0, "", fmt.Errorf("%w: %s", errPortNotValid, line)
|
||||
return netip.Addr{}, 0, "", fmt.Errorf("port is not valid: %s", line)
|
||||
} else if portInt < 1 || portInt > 65535 {
|
||||
return netip.Addr{}, 0, "", fmt.Errorf("%w: %d must be between 1 and 65535", errPortNotValid, portInt)
|
||||
return netip.Addr{}, 0, "", fmt.Errorf("port is not valid: %d must be between 1 and 65535", portInt)
|
||||
}
|
||||
port = uint16(portInt)
|
||||
}
|
||||
@@ -154,20 +142,18 @@ func extractRemote(line string) (ip netip.Addr, port uint16,
|
||||
return ip, port, protocol, nil
|
||||
}
|
||||
|
||||
var errPostLineFieldsCount = errors.New("post line has not 2 fields as expected")
|
||||
|
||||
func extractPort(line string) (port uint16, err error) {
|
||||
fields := strings.Fields(line)
|
||||
const expectedFieldsCount = 2
|
||||
if len(fields) != expectedFieldsCount {
|
||||
return 0, fmt.Errorf("%w: %s", errPostLineFieldsCount, line)
|
||||
return 0, fmt.Errorf("post line has not 2 fields as expected: %s", line)
|
||||
}
|
||||
|
||||
portInt, err := strconv.Atoi(fields[1])
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%w: %s", errPortNotValid, line)
|
||||
return 0, fmt.Errorf("port is not valid: %s", line)
|
||||
} else if portInt < 1 || portInt > 65535 {
|
||||
return 0, fmt.Errorf("%w: %d must be between 1 and 65535", errPortNotValid, portInt)
|
||||
return 0, fmt.Errorf("port is not valid: %d must be between 1 and 65535", portInt)
|
||||
}
|
||||
port = uint16(portInt)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user