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 -7
View File
@@ -11,14 +11,11 @@ import (
"strconv"
"time"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/provider/utils"
)
var regexPort = regexp.MustCompile(`[1-9][0-9]{0,4}`)
var ErrPortForwardedNotFound = errors.New("port forwarded not found")
// PortForward obtains a VPN server side port forwarded from the PrivateVPN API.
// It returns 0 if all ports are to forwarded on a dedicated server IP.
func (p *Provider) PortForward(ctx context.Context, objects utils.PortForwardObjects) (
@@ -42,8 +39,7 @@ func (p *Provider) PortForward(ctx context.Context, objects utils.PortForwardObj
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %d %s", common.ErrHTTPStatusCodeNotOK,
response.StatusCode, response.Status)
return nil, fmt.Errorf("HTTP status code not OK: %d %s", response.StatusCode, response.Status)
}
defer response.Body.Close()
@@ -62,12 +58,12 @@ func (p *Provider) PortForward(ctx context.Context, objects utils.PortForwardObj
return nil, fmt.Errorf("decoding JSON response: %w; data is: %s",
err, string(bytes))
} else if !data.Supported {
return nil, fmt.Errorf("%w for this VPN server", common.ErrPortForwardNotSupported)
return nil, errors.New("port forwarding not supported for this VPN server")
}
portString := regexPort.FindString(data.Status)
if portString == "" {
return nil, fmt.Errorf("%w: in status %q", ErrPortForwardedNotFound, data.Status)
return nil, fmt.Errorf("port forwarded not found in status %q", data.Status)
}
const base, bitSize = 10, 16
@@ -22,8 +22,6 @@ func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
func Test_Provider_PortForward(t *testing.T) {
t.Parallel()
errTest := errors.New("test error")
canceledCtx, cancel := context.WithCancel(context.Background())
cancel()
@@ -59,7 +57,7 @@ func Test_Provider_PortForward(t *testing.T) {
assert.Equal(t,
"https://connect.pvdatanet.com/v3/Api/port?ip[]=10.10.10.10",
r.URL.String())
return nil, errTest
return nil, errors.New("test error")
}),
},
},
@@ -156,7 +154,7 @@ func Test_Provider_PortForward(t *testing.T) {
}),
},
},
errMessage: "port forwarded not found: in status \"no port here\"",
errMessage: "port forwarded not found in status \"no port here\"",
},
"port_too_big": {
ctx: context.Background(),
@@ -1,7 +1,6 @@
package updater
import (
"errors"
"fmt"
"regexp"
"strings"
@@ -9,12 +8,6 @@ import (
var trailingNumber = regexp.MustCompile(` [0-9]+$`)
var (
errBadPrefix = errors.New("bad prefix in file name")
errBadSuffix = errors.New("bad suffix in file name")
errNotEnoughParts = errors.New("not enough parts in file name")
)
func parseFilename(fileName string) (
countryCode, city string, err error,
) {
@@ -22,7 +15,7 @@ func parseFilename(fileName string) (
const prefix = "PrivateVPN-"
if !strings.HasPrefix(fileName, prefix) {
return "", "", fmt.Errorf("%w: %s", errBadPrefix, fileName)
return "", "", fmt.Errorf("bad prefix in file name %s", fileName)
}
s := strings.TrimPrefix(fileName, prefix)
@@ -34,7 +27,7 @@ func parseFilename(fileName string) (
case strings.HasSuffix(fileName, udpSuffix):
s = strings.TrimSuffix(s, udpSuffix)
default:
return "", "", fmt.Errorf("%w: %s", errBadSuffix, fileName)
return "", "", fmt.Errorf("bad suffix in file name %s", fileName)
}
s = trailingNumber.ReplaceAllString(s, "")
@@ -42,7 +35,7 @@ func parseFilename(fileName string) (
parts := strings.Split(s, "-")
const minParts = 2
if len(parts) < minParts {
return "", "", fmt.Errorf("%w: %s", errNotEnoughParts, fileName)
return "", "", fmt.Errorf("not enough parts in file name %s", fileName)
}
countryCode, city = parts[0], parts[1]