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:
@@ -21,7 +21,7 @@ func settingsToLookupMap(settings Settings) (routeToRoles map[string][]internalR
|
||||
case AuthBasic:
|
||||
checker = newBasicAuthMethod(role.Username, role.Password)
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: %s", ErrMethodNotSupported, role.Auth)
|
||||
return nil, fmt.Errorf("authentication method not supported: %s", role.Auth)
|
||||
}
|
||||
|
||||
iRole := internalRole{
|
||||
|
||||
@@ -13,7 +13,6 @@ func Test_settingsToLookupMap(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
settings Settings
|
||||
routeToRoles map[string][]internalRole
|
||||
errWrapped error
|
||||
errMessage string
|
||||
}{
|
||||
"empty_settings": {
|
||||
@@ -23,7 +22,6 @@ func Test_settingsToLookupMap(t *testing.T) {
|
||||
settings: Settings{
|
||||
Roles: []Role{{Name: "a", Auth: "bad"}},
|
||||
},
|
||||
errWrapped: ErrMethodNotSupported,
|
||||
errMessage: "authentication method not supported: bad",
|
||||
},
|
||||
"success": {
|
||||
@@ -51,9 +49,10 @@ func Test_settingsToLookupMap(t *testing.T) {
|
||||
routeToRoles, err := settingsToLookupMap(testCase.settings)
|
||||
|
||||
assert.Equal(t, testCase.routeToRoles, routeToRoles)
|
||||
assert.ErrorIs(t, err, testCase.errWrapped)
|
||||
if testCase.errWrapped != nil {
|
||||
if testCase.errMessage != "" {
|
||||
assert.EqualError(t, err, testCase.errMessage)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package auth
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
@@ -106,28 +105,19 @@ type Role struct {
|
||||
Routes []string `json:"-"`
|
||||
}
|
||||
|
||||
var (
|
||||
ErrMethodNotSupported = errors.New("authentication method not supported")
|
||||
ErrAPIKeyEmpty = errors.New("api key is empty")
|
||||
ErrBasicUsernameEmpty = errors.New("username is empty")
|
||||
ErrBasicPasswordEmpty = errors.New("password is empty")
|
||||
ErrRoutePathNotSupported = errors.New("route path not supported by the control server")
|
||||
ErrRouteMethodNotSupported = errors.New("route method not supported for the path")
|
||||
)
|
||||
|
||||
func (r Role) Validate() (err error) {
|
||||
err = validate.IsOneOf(r.Auth, AuthNone, AuthAPIKey, AuthBasic)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %s", ErrMethodNotSupported, r.Auth)
|
||||
return fmt.Errorf("authentication method not supported: %s", r.Auth)
|
||||
}
|
||||
|
||||
switch {
|
||||
case r.Auth == AuthAPIKey && r.APIKey == "":
|
||||
return fmt.Errorf("for role %s: %w", r.Name, ErrAPIKeyEmpty)
|
||||
return fmt.Errorf("for role %s: api key is empty", r.Name)
|
||||
case r.Auth == AuthBasic && r.Username == "":
|
||||
return fmt.Errorf("for role %s: %w", r.Name, ErrBasicUsernameEmpty)
|
||||
return fmt.Errorf("for role %s: username is empty", r.Name)
|
||||
case r.Auth == AuthBasic && r.Password == "":
|
||||
return fmt.Errorf("for role %s: %w", r.Name, ErrBasicPasswordEmpty)
|
||||
return fmt.Errorf("for role %s: password is empty", r.Name)
|
||||
}
|
||||
|
||||
for i, route := range r.Routes {
|
||||
@@ -136,11 +126,11 @@ func (r Role) Validate() (err error) {
|
||||
method, path := parts[0], parts[1]
|
||||
methods, ok := validRoutes[path]
|
||||
if !ok {
|
||||
return fmt.Errorf("route %d of %d: %w: %s",
|
||||
i+1, len(r.Routes), ErrRoutePathNotSupported, path)
|
||||
return fmt.Errorf("route %d of %d: route path not supported by the control server: %s",
|
||||
i+1, len(r.Routes), path)
|
||||
} else if !slices.Contains(methods, method) {
|
||||
return fmt.Errorf("route %d of %d: %w: %s for path %s",
|
||||
i+1, len(r.Routes), ErrRouteMethodNotSupported, method, path)
|
||||
return fmt.Errorf("route %d of %d: route method not supported for the path: %s for path %s",
|
||||
i+1, len(r.Routes), method, path)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/qdm12/gluetun/internal/constants"
|
||||
@@ -12,16 +11,14 @@ type statusWrapper struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
var errInvalidStatus = errors.New("invalid status")
|
||||
|
||||
func (sw *statusWrapper) getStatus() (status models.LoopStatus, err error) {
|
||||
status = models.LoopStatus(sw.Status)
|
||||
switch status {
|
||||
case constants.Stopped, constants.Running:
|
||||
return status, nil
|
||||
default:
|
||||
return "", fmt.Errorf("%w: %s: possible values are: %s, %s",
|
||||
errInvalidStatus, sw.Status, constants.Stopped, constants.Running)
|
||||
return "", fmt.Errorf("invalid status: %s: possible values are: %s, %s",
|
||||
sw.Status, constants.Stopped, constants.Running)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user