Skip to content

Commit

Permalink
lxd/config: Update error messages
Browse files Browse the repository at this point in the history
Signed-off-by: Julian Pelizäus <julian.pelizaeus@canonical.com>
  • Loading branch information
roosterfish committed May 24, 2024
1 parent a2426b7 commit 288f85f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lxd/config/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type Error struct {

// Error implements the error interface.
func (e Error) Error() string {
message := fmt.Sprintf("cannot set '%s'", e.Name)
message := fmt.Sprintf("Cannot set %q", e.Name)
if e.Value != nil {
message += fmt.Sprintf(" to '%v'", e.Value)
message += fmt.Sprintf(" to %q", e.Value)
}

return message + fmt.Sprintf(": %s", e.Reason)
Expand All @@ -30,7 +30,7 @@ type ErrorList []*Error
func (l ErrorList) Error() string {
switch len(l) {
case 0:
return "no errors"
return "No errors"
case 1:
return l[0].Error()
}
Expand Down
8 changes: 4 additions & 4 deletions lxd/config/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func (m *Map) Change(changes map[string]any) (map[string]string, error) {
// Ensure that we were actually passed a string.
s := reflect.ValueOf(change)
if s.Kind() != reflect.String {
errors.add(name, nil, fmt.Sprintf("invalid type %s", s.Kind()))
errors.add(name, nil, fmt.Sprintf("Invalid type: %q", s.Kind()))
continue
}

value, ok := change.(string)
if !ok {
errors.add(name, nil, fmt.Sprintf("failed to cast value %q to string", change))
errors.add(name, nil, fmt.Sprintf("Failed to cast value %q to string", change))
continue
}

Expand Down Expand Up @@ -145,7 +145,7 @@ func (m *Map) GetInt64(name string) int64 {
m.schema.assertKeyType(name, Int64)
n, err := strconv.ParseInt(m.GetRaw(name), 10, 64)
if err != nil {
panic(fmt.Sprintf("cannot convert to int64: %v", err))
panic(fmt.Sprintf("Cannot convert to int64: %v", err))
}

return n
Expand Down Expand Up @@ -219,7 +219,7 @@ func (m *Map) set(name string, value string, initial bool) (bool, error) {

key, ok := m.schema[name]
if !ok {
return false, fmt.Errorf("unknown key")
return false, fmt.Errorf("Unknown key")
}

// When unsetting a config key, the value argument will be empty.
Expand Down
12 changes: 6 additions & 6 deletions lxd/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s Schema) Defaults() map[string]any {
func (s Schema) mustGetKey(name string) Key {
key, ok := s[name]
if !ok {
panic(fmt.Sprintf("attempt to access unknown key '%s'", name))
panic(fmt.Sprintf("Attempt to access unknown key %q", name))
}

return key
Expand All @@ -52,7 +52,7 @@ func (s Schema) mustGetKey(name string) Key {
func (s Schema) assertKeyType(name string, code Type) {
key := s.mustGetKey(name)
if key.Type != code {
panic(fmt.Sprintf("key '%s' has type code %d, not %d", name, key.Type, code))
panic(fmt.Sprintf("Key %q has type code %d, not %d", name, key.Type, code))
}
}

Expand Down Expand Up @@ -102,21 +102,21 @@ func (v *Key) validate(value string) error {
case String:
case Bool:
if !shared.ValueInSlice(strings.ToLower(value), booleans) {
return fmt.Errorf("invalid boolean")
return fmt.Errorf("Invalid boolean")
}

case Int64:
_, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return fmt.Errorf("invalid integer")
return fmt.Errorf("Invalid integer")
}

default:
panic(fmt.Sprintf("unexpected value type: %d", v.Type))
panic(fmt.Sprintf("Unexpected value type: %d", v.Type))
}

if v.Deprecated != "" && value != v.Default {
return fmt.Errorf("deprecated: %s", v.Deprecated)
return fmt.Errorf("Deprecated: %q", v.Deprecated)
}

// Run external validation function
Expand Down

0 comments on commit 288f85f

Please sign in to comment.