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
+1 -4
View File
@@ -2,15 +2,12 @@ package html
import (
"context"
"errors"
"fmt"
"net/http"
"golang.org/x/net/html"
)
var ErrHTTPStatusCodeNotOK = errors.New("HTTP status code is not OK")
func Fetch(ctx context.Context, client *http.Client, url string) (
rootNode *html.Node, err error,
) {
@@ -25,7 +22,7 @@ func Fetch(ctx context.Context, client *http.Client, url string) (
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %d %s", ErrHTTPStatusCodeNotOK,
return nil, fmt.Errorf("HTTP status code not OK: %d %s",
response.StatusCode, response.Status)
}
+4 -6
View File
@@ -37,21 +37,18 @@ func Test_Fetch(t *testing.T) {
responseStatus int
responseBody io.ReadCloser
rootNode *html.Node
errWrapped error
errMessage string
}{
"context canceled": {
ctx: canceledCtx,
url: "https://example.com/path",
errWrapped: context.Canceled,
errMessage: `Get "https://example.com/path": context canceled`,
},
"response status not ok": {
ctx: context.Background(),
url: "https://example.com/path",
responseStatus: http.StatusNotFound,
errWrapped: ErrHTTPStatusCodeNotOK,
errMessage: `HTTP status code is not OK: 404 Not Found`,
errMessage: `HTTP status code not OK: 404 Not Found`,
},
"success": {
ctx: context.Background(),
@@ -86,9 +83,10 @@ func Test_Fetch(t *testing.T) {
rootNode, err := Fetch(testCase.ctx, client, testCase.url)
assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
if testCase.errMessage != "" {
assert.EqualError(t, err, testCase.errMessage)
} else {
assert.NoError(t, err)
}
assert.Equal(t, testCase.rootNode, rootNode)
})
+2 -5
View File
@@ -2,7 +2,6 @@ package loop
import (
"context"
"errors"
"fmt"
"reflect"
"sync"
@@ -31,8 +30,6 @@ func (l *Loop) GetStatus() (status models.LoopStatus) {
return l.state.status
}
var ErrInvalidStatus = errors.New("invalid status")
func (l *Loop) SetStatus(ctx context.Context, status models.LoopStatus) (outcome string, err error) {
l.state.statusMu.Lock()
defer l.state.statusMu.Unlock()
@@ -79,8 +76,8 @@ func (l *Loop) SetStatus(ctx context.Context, status models.LoopStatus) (outcome
l.state.status = newStatus
return status.String(), nil
default:
return "", fmt.Errorf("%w: %s: it can only be one of: %s, %s",
ErrInvalidStatus, status, constants.Running, constants.Stopped)
return "", fmt.Errorf("invalid status: %s: it can only be one of: %s, %s",
status, constants.Running, constants.Stopped)
}
}
+3 -9
View File
@@ -8,12 +8,6 @@ import (
"strings"
)
var (
ErrUnknownProto = errors.New("unknown protocol")
ErrNoRemoteHost = errors.New("remote host not found")
ErrNoRemoteIP = errors.New("remote IP not found")
)
func ExtractProto(b []byte) (tcp, udp bool, err error) {
lines := strings.Split(string(b), "\n")
const protoPrefix = "proto "
@@ -30,7 +24,7 @@ func ExtractProto(b []byte) (tcp, udp bool, err error) {
case "udp", "udp4", "udp6":
return false, true, nil
default:
return false, false, fmt.Errorf("%w: %s", ErrUnknownProto, s)
return false, false, fmt.Errorf("unknown protocol: %s", s)
}
}
@@ -45,7 +39,7 @@ func ExtractHost(b []byte) (host, warning string, err error) {
)
hosts := extractRemoteHosts(b, rejectIP, rejectDomain)
if len(hosts) == 0 {
return "", "", ErrNoRemoteHost
return "", "", errors.New("remote host not found")
} else if len(hosts) > 1 {
warning = fmt.Sprintf(
"only using the first host %q and discarding %d other hosts",
@@ -58,7 +52,7 @@ func ExtractIPs(b []byte) (ips []netip.Addr, err error) {
const rejectIP, rejectDomain = false, true
ipStrings := extractRemoteHosts(b, rejectIP, rejectDomain)
if len(ipStrings) == 0 {
return nil, ErrNoRemoteIP
return nil, errors.New("remote IP not found")
}
sort.Slice(ipStrings, func(i, j int) bool {
+1 -1
View File
@@ -19,7 +19,7 @@ func FetchFile(ctx context.Context, client *http.Client, url string) (
const rejectDomain = false
hosts := extractRemoteHosts(b, rejectIP, rejectDomain)
if len(hosts) == 0 {
return "", fmt.Errorf("%w for url %s", ErrNoRemoteHost, url)
return "", fmt.Errorf("remote host not found for url %s", url)
}
return hosts[0], nil
-2
View File
@@ -15,8 +15,6 @@ type Provider interface {
FetchServers(ctx context.Context, minServers int) (servers []models.Server, err error)
}
var ErrServerHasNotEnoughInformation = errors.New("server has not enough information")
func (u *Updater) updateProvider(ctx context.Context, provider Provider,
minRatio float64,
) (err error) {
+1 -7
View File
@@ -2,7 +2,6 @@ package resolver
import (
"context"
"errors"
"fmt"
"net/netip"
)
@@ -34,11 +33,6 @@ type parallelResult struct {
IPs []netip.Addr
}
var (
ErrMinFound = errors.New("not enough hosts found")
ErrMaxFailRatio = errors.New("maximum failure ratio reached")
)
func (pr *Parallel) Resolve(ctx context.Context, settings ParallelSettings) (
hostToIPs map[string][]netip.Addr, warnings []string, err error,
) {
@@ -90,7 +84,7 @@ func (pr *Parallel) Resolve(ctx context.Context, settings ParallelSettings) (
failureRatio := float64(len(warnings)) / float64(len(settings.Hosts))
if failureRatio > settings.MaxFailRatio {
return hostToIPs, warnings,
fmt.Errorf("%w: %.2f failure ratio reached", ErrMaxFailRatio, failureRatio)
fmt.Errorf("maximum failure ratio reached: %.2f failure ratio reached", failureRatio)
}
return hostToIPs, warnings, nil
+5 -10
View File
@@ -2,7 +2,6 @@ package resolver
import (
"context"
"errors"
"fmt"
"net"
"net/netip"
@@ -62,11 +61,6 @@ func (r *Repeat) Resolve(ctx context.Context, host string, settings RepeatSettin
return ips, nil
}
var (
ErrMaxNoNew = errors.New("reached the maximum number of no new update")
ErrMaxFails = errors.New("reached the maximum number of consecutive failures")
)
func (r *Repeat) resolveOnce(ctx, timedCtx context.Context, host string,
settings RepeatSettings, uniqueIPs map[string]struct{}, noNewCounter, failCounter int) (
newNoNewCounter, newFailCounter int, err error,
@@ -75,8 +69,8 @@ func (r *Repeat) resolveOnce(ctx, timedCtx context.Context, host string,
if err != nil {
failCounter++
if settings.MaxFails > 0 && failCounter == settings.MaxFails {
return noNewCounter, failCounter, fmt.Errorf("%w: %d failed attempts resolving %s: %s",
ErrMaxFails, settings.MaxFails, host, err)
return noNewCounter, failCounter, fmt.Errorf("reached the maximum number of consecutive failures: "+
"%d failed attempts resolving %s: %s", settings.MaxFails, host, err)
}
// it's fine to fail some of the resolutions
return noNewCounter, failCounter, nil
@@ -100,8 +94,9 @@ func (r *Repeat) resolveOnce(ctx, timedCtx context.Context, host string,
// we reached the maximum number of resolutions without
// finding any new IP address to our unique IP addresses set.
return noNewCounter, failCounter,
fmt.Errorf("%w: %d times no updated for %d IP addresses found",
ErrMaxNoNew, noNewCounter, len(uniqueIPs))
fmt.Errorf("reached the maximum number of no new update: "+
"%d times no updated for %d IP addresses found",
noNewCounter, len(uniqueIPs))
}
timer := time.NewTimer(settings.BetweenDuration)
+1 -4
View File
@@ -2,14 +2,11 @@ package unzip
import (
"context"
"errors"
"fmt"
"io"
"net/http"
)
var ErrHTTPStatusCodeNotOK = errors.New("HTTP status code not OK")
func (u *Unzipper) FetchAndExtract(ctx context.Context, url string) (
contents map[string][]byte, err error,
) {
@@ -26,7 +23,7 @@ func (u *Unzipper) FetchAndExtract(ctx context.Context, url string) (
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %s: %d %s", ErrHTTPStatusCodeNotOK,
return nil, fmt.Errorf("HTTP status code not OK: %s: %d %s",
url, response.StatusCode, response.Status)
}