errors: rewrite errors to follow go best practices

Errors should not start capitalised and they should not contain the word error
or state that they "failed" as we already know it is an error

Signed-off-by: Kristoffer Dalby <kristoffer@dalby.cc>
This commit is contained in:
Kristoffer Dalby 2026-02-05 16:29:54 +00:00
parent 4a9a329339
commit 3acce2da87
30 changed files with 300 additions and 300 deletions

View file

@ -138,7 +138,7 @@ func (u Username) Validate() error {
if isUser(string(u)) {
return nil
}
return fmt.Errorf("Username has to contain @, got: %q", u)
return fmt.Errorf("username must contain @, got: %q", u)
}
func (u *Username) String() string {
@ -243,7 +243,7 @@ func (g Group) Validate() error {
if isGroup(string(g)) {
return nil
}
return fmt.Errorf(`Group has to start with "group:", got: %q`, g)
return fmt.Errorf(`group must start with "group:", got: %q`, g)
}
func (g *Group) UnmarshalJSON(b []byte) error {
@ -354,7 +354,7 @@ func (h Host) Validate() error {
if isHost(string(h)) {
return nil
}
return fmt.Errorf("Hostname %q is invalid", h)
return fmt.Errorf("hostname %q is invalid", h)
}
func (h *Host) UnmarshalJSON(b []byte) error {
@ -372,7 +372,7 @@ func (h Host) Resolve(p *Policy, _ types.Users, nodes views.Slice[types.NodeView
pref, ok := p.Hosts[h]
if !ok {
return nil, fmt.Errorf("unable to resolve host: %q", h)
return nil, fmt.Errorf("resolving host: %q", h)
}
err := pref.Validate()
if err != nil {
@ -406,7 +406,7 @@ func (p Prefix) Validate() error {
if netip.Prefix(p).IsValid() {
return nil
}
return fmt.Errorf("Prefix %q is invalid", p)
return fmt.Errorf("prefix %q is invalid", p)
}
func (p Prefix) String() string {
@ -505,7 +505,7 @@ func (ag AutoGroup) Validate() error {
return nil
}
return fmt.Errorf("AutoGroup is invalid, got: %q, must be one of %v", ag, autogroups)
return fmt.Errorf("autogroup is invalid, got: %q, must be one of %v", ag, autogroups)
}
func (ag *AutoGroup) UnmarshalJSON(b []byte) error {
@ -1003,14 +1003,14 @@ func (g *Groups) UnmarshalJSON(b []byte) error {
if str, ok := item.(string); ok {
stringSlice = append(stringSlice, str)
} else {
return fmt.Errorf(`Group "%s" contains invalid member type, expected string but got %T`, key, item)
return fmt.Errorf(`group "%s" contains invalid member type, expected string but got %T`, key, item)
}
}
rawGroups[key] = stringSlice
case string:
return fmt.Errorf(`Group "%s" value must be an array of users, got string: "%s"`, key, v)
return fmt.Errorf(`group "%s" value must be an array of users, got string: "%s"`, key, v)
default:
return fmt.Errorf(`Group "%s" value must be an array of users, got %T`, key, v)
return fmt.Errorf(`group "%s" value must be an array of users, got %T`, key, v)
}
}
@ -1024,7 +1024,7 @@ func (g *Groups) UnmarshalJSON(b []byte) error {
username := Username(u)
if err := username.Validate(); err != nil {
if isGroup(u) {
return fmt.Errorf("Nested groups are not allowed, found %q inside %q", u, group)
return fmt.Errorf("nested groups are not allowed, found %q inside %q", u, group)
}
return err
@ -1056,7 +1056,7 @@ func (h *Hosts) UnmarshalJSON(b []byte) error {
var prefix Prefix
if err := prefix.parseString(value); err != nil {
return fmt.Errorf(`Hostname "%s" contains an invalid IP address: "%s"`, key, value)
return fmt.Errorf(`hostname "%s" contains an invalid IP address: "%s"`, key, value)
}
(*h)[host] = prefix
@ -1128,7 +1128,7 @@ func (to TagOwners) Contains(tagOwner *Tag) error {
}
}
return fmt.Errorf(`Tag %q is not defined in the Policy, please define or remove the reference to it`, tagOwner)
return fmt.Errorf(`tag %q is not defined in the policy, please define or remove the reference to it`, tagOwner)
}
type AutoApproverPolicy struct {
@ -1750,7 +1750,7 @@ func (p *Policy) validate() error {
case *Host:
h := src
if !p.Hosts.exist(*h) {
errs = append(errs, fmt.Errorf(`Host %q is not defined in the Policy, please define or remove the reference to it`, *h))
errs = append(errs, fmt.Errorf(`host %q is not defined in the policy, please define or remove the reference to it`, *h))
}
case *AutoGroup:
ag := src
@ -1782,7 +1782,7 @@ func (p *Policy) validate() error {
case *Host:
h := dst.Alias.(*Host)
if !p.Hosts.exist(*h) {
errs = append(errs, fmt.Errorf(`Host %q is not defined in the Policy, please define or remove the reference to it`, *h))
errs = append(errs, fmt.Errorf(`host %q is not defined in the policy, please define or remove the reference to it`, *h))
}
case *AutoGroup:
ag := dst.Alias.(*AutoGroup)

View file

@ -380,7 +380,7 @@ func TestUnmarshalPolicy(t *testing.T) {
},
}
`,
wantErr: `Username has to contain @, got: "invalid"`,
wantErr: `username must contain @, got: "invalid"`,
},
{
name: "invalid-group",
@ -393,7 +393,7 @@ func TestUnmarshalPolicy(t *testing.T) {
},
}
`,
wantErr: `Group has to start with "group:", got: "grou:example"`,
wantErr: `group must start with "group:", got: "grou:example"`,
},
{
name: "group-in-group",
@ -407,8 +407,8 @@ func TestUnmarshalPolicy(t *testing.T) {
},
}
`,
// wantErr: `Username has to contain @, got: "group:inner"`,
wantErr: `Nested groups are not allowed, found "group:inner" inside "group:example"`,
// wantErr: `username must contain @, got: "group:inner"`,
wantErr: `nested groups are not allowed, found "group:inner" inside "group:example"`,
},
{
name: "invalid-addr",
@ -419,7 +419,7 @@ func TestUnmarshalPolicy(t *testing.T) {
},
}
`,
wantErr: `Hostname "derp" contains an invalid IP address: "10.0"`,
wantErr: `hostname "derp" contains an invalid IP address: "10.0"`,
},
{
name: "invalid-prefix",
@ -430,7 +430,7 @@ func TestUnmarshalPolicy(t *testing.T) {
},
}
`,
wantErr: `Hostname "derp" contains an invalid IP address: "10.0/42"`,
wantErr: `hostname "derp" contains an invalid IP address: "10.0/42"`,
},
// TODO(kradalby): Figure out why this doesn't work.
// {
@ -459,7 +459,7 @@ func TestUnmarshalPolicy(t *testing.T) {
],
}
`,
wantErr: `AutoGroup is invalid, got: "autogroup:invalid", must be one of [autogroup:internet autogroup:member autogroup:nonroot autogroup:tagged autogroup:self]`,
wantErr: `autogroup is invalid, got: "autogroup:invalid", must be one of [autogroup:internet autogroup:member autogroup:nonroot autogroup:tagged autogroup:self]`,
},
{
name: "undefined-hostname-errors-2490",
@ -478,7 +478,7 @@ func TestUnmarshalPolicy(t *testing.T) {
]
}
`,
wantErr: `Host "user1" is not defined in the Policy, please define or remove the reference to it`,
wantErr: `host "user1" is not defined in the policy, please define or remove the reference to it`,
},
{
name: "defined-hostname-does-not-err-2490",
@ -854,7 +854,7 @@ func TestUnmarshalPolicy(t *testing.T) {
]
}
`,
wantErr: `Tag "tag:notdefined" is not defined in the Policy, please define or remove the reference to it`,
wantErr: `tag "tag:notdefined" is not defined in the policy, please define or remove the reference to it`,
},
{
name: "tag-must-be-defined-acl-dst",
@ -873,7 +873,7 @@ func TestUnmarshalPolicy(t *testing.T) {
]
}
`,
wantErr: `Tag "tag:notdefined" is not defined in the Policy, please define or remove the reference to it`,
wantErr: `tag "tag:notdefined" is not defined in the policy, please define or remove the reference to it`,
},
{
name: "tag-must-be-defined-acl-ssh-src",
@ -892,7 +892,7 @@ func TestUnmarshalPolicy(t *testing.T) {
]
}
`,
wantErr: `Tag "tag:notdefined" is not defined in the Policy, please define or remove the reference to it`,
wantErr: `tag "tag:notdefined" is not defined in the policy, please define or remove the reference to it`,
},
{
name: "tag-must-be-defined-acl-ssh-dst",
@ -914,7 +914,7 @@ func TestUnmarshalPolicy(t *testing.T) {
]
}
`,
wantErr: `Tag "tag:notdefined" is not defined in the Policy, please define or remove the reference to it`,
wantErr: `tag "tag:notdefined" is not defined in the policy, please define or remove the reference to it`,
},
{
name: "tag-must-be-defined-acl-autoapprover-route",
@ -927,7 +927,7 @@ func TestUnmarshalPolicy(t *testing.T) {
},
}
`,
wantErr: `Tag "tag:notdefined" is not defined in the Policy, please define or remove the reference to it`,
wantErr: `tag "tag:notdefined" is not defined in the policy, please define or remove the reference to it`,
},
{
name: "tag-must-be-defined-acl-autoapprover-exitnode",
@ -938,7 +938,7 @@ func TestUnmarshalPolicy(t *testing.T) {
},
}
`,
wantErr: `Tag "tag:notdefined" is not defined in the Policy, please define or remove the reference to it`,
wantErr: `tag "tag:notdefined" is not defined in the policy, please define or remove the reference to it`,
},
{
name: "missing-dst-port-is-err",
@ -1010,7 +1010,7 @@ func TestUnmarshalPolicy(t *testing.T) {
}
}
`,
wantErr: `Group has to start with "group:", got: "INVALID_GROUP_FIELD"`,
wantErr: `group must start with "group:", got: "INVALID_GROUP_FIELD"`,
},
{
name: "invalid-group-datatype",
@ -1022,7 +1022,7 @@ func TestUnmarshalPolicy(t *testing.T) {
}
}
`,
wantErr: `Group "group:invalid" value must be an array of users, got string: "should fail"`,
wantErr: `group "group:invalid" value must be an array of users, got string: "should fail"`,
},
{
name: "invalid-group-name-and-datatype-fails-on-name-first",
@ -1034,7 +1034,7 @@ func TestUnmarshalPolicy(t *testing.T) {
}
}
`,
wantErr: `Group has to start with "group:", got: "INVALID_GROUP_FIELD"`,
wantErr: `group must start with "group:", got: "INVALID_GROUP_FIELD"`,
},
{
name: "disallow-unsupported-fields-hosts-level",
@ -1046,7 +1046,7 @@ func TestUnmarshalPolicy(t *testing.T) {
}
}
`,
wantErr: `Hostname "INVALID_HOST_FIELD" contains an invalid IP address: "should fail"`,
wantErr: `hostname "INVALID_HOST_FIELD" contains an invalid IP address: "should fail"`,
},
{
name: "disallow-unsupported-fields-tagowners-level",
@ -2045,7 +2045,7 @@ func TestResolvePolicy(t *testing.T) {
"testhost": p("100.100.101.102/32"),
},
},
wantErr: `unable to resolve host: "invalidhost"`,
wantErr: `resolving host: "invalidhost"`,
},
{
name: "multiple-groups",
@ -2909,7 +2909,7 @@ func TestNodeCanHaveTag(t *testing.T) {
node: nodes[0],
tag: "tag:test",
want: false,
wantErr: "Username has to contain @",
wantErr: "username must contain @",
},
{
name: "node-cannot-have-tag",