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
+4 -9
View File
@@ -3,17 +3,11 @@ package natpmp
import (
"context"
"encoding/binary"
"errors"
"fmt"
"net/netip"
"time"
)
var (
ErrNetworkProtocolUnknown = errors.New("network protocol is unknown")
ErrLifetimeTooLong = errors.New("lifetime is too long")
)
// Add or delete a port mapping. To delete a mapping, set both the
// requestedExternalPort and lifetime to 0.
// See https://www.ietf.org/rfc/rfc6886.html#section-3.3
@@ -26,8 +20,9 @@ func (c *Client) AddPortMapping(ctx context.Context, gateway netip.Addr,
lifetimeSecondsFloat := lifetime.Seconds()
const maxLifetimeSeconds = uint64(^uint32(0))
if uint64(lifetimeSecondsFloat) > maxLifetimeSeconds {
return 0, 0, 0, 0, fmt.Errorf("%w: %d seconds must at most %d seconds",
ErrLifetimeTooLong, uint64(lifetimeSecondsFloat), maxLifetimeSeconds)
return 0, 0, 0, 0, fmt.Errorf("lifetime is too long: "+
"%d seconds must at most %d seconds",
uint64(lifetimeSecondsFloat), maxLifetimeSeconds)
}
const messageSize = 12
message := make([]byte, messageSize)
@@ -38,7 +33,7 @@ func (c *Client) AddPortMapping(ctx context.Context, gateway netip.Addr,
case "tcp":
message[1] = 2 // operationCode 2
default:
return 0, 0, 0, 0, fmt.Errorf("%w: %s", ErrNetworkProtocolUnknown, protocol)
return 0, 0, 0, 0, fmt.Errorf("network protocol is unknown: %s", protocol)
}
// [2:3] are reserved.
binary.BigEndian.PutUint16(message[4:6], internalPort)