use of typeName() in error messages

This commit is contained in:
Celestino Amoroso 2024-06-05 05:48:02 +02:00
parent 974835a8ef
commit ab2e3f0528
3 changed files with 13 additions and 19 deletions

View File

@ -25,15 +25,11 @@ func errTooMuchParams(funcName string, maxArgs, argCount int) (err error) {
// --- General errors
func errCantConvert(funcName string, value any, kind string) error {
if typer, ok := value.(Typer); ok {
return fmt.Errorf("%s(): can't convert %s to %s", funcName, typer.TypeName(), kind)
} else {
return fmt.Errorf("%s(): can't convert %T to %s", funcName, value, kind)
}
return fmt.Errorf("%s(): can't convert %s to %s", funcName, typeName(value), kind)
}
func errExpectedGot(funcName string, kind string, value any) error {
return fmt.Errorf("%s() expected %s, got %T (%v)", funcName, kind, value, value)
return fmt.Errorf("%s() expected %s, got %s (%v)", funcName, kind, typeName(value), value)
}
func errFuncDivisionByZero(funcName string) error {
@ -46,18 +42,14 @@ func errDivisionByZero() error {
// --- Parameter errors
// func errOneParam(funcName string) error {
// return fmt.Errorf("%s() requires exactly one param", funcName)
// }
func errMissingRequiredParameter(funcName, paramName string) error {
return fmt.Errorf("%s() missing required parameter %q", funcName, paramName)
}
func errInvalidParameterValue(funcName, paramName string, paramValue any) error {
return fmt.Errorf("%s() invalid value %T (%v) for parameter %q", funcName, paramValue, paramValue, paramName)
return fmt.Errorf("%s() invalid value %s (%v) for parameter %q", funcName, typeName(paramValue), paramValue, paramName)
}
func errWrongParamType(funcName, paramName, paramType string, paramValue any) error {
return fmt.Errorf("%s() the %q parameter must be a %s, got a %T (%v)", funcName, paramName, paramType, paramValue, paramValue)
return fmt.Errorf("%s() the %q parameter must be a %s, got a %s (%v)", funcName, paramName, paramType, typeName(paramValue), paramValue)
}

View File

@ -51,8 +51,10 @@ type Typer interface {
TypeName() string
}
func getTypeName(v any) (name string) {
if typer, ok := v.(Typer); ok {
func typeName(v any) (name string) {
if v == nil {
name = "nil"
} else if typer, ok := v.(Typer); ok {
name = typer.TypeName()
} else if IsInteger(v) {
name = "integer"

10
term.go
View File

@ -156,15 +156,15 @@ func (self *term) toInt(computedValue any, valueDescription string) (i int, err
if index64, ok := computedValue.(int64); ok {
i = int(index64)
} else {
err = self.Errorf("%s, got %T (%v)", valueDescription, computedValue, computedValue)
err = self.Errorf("%s, got %s (%v)", valueDescription, typeName(computedValue), computedValue)
}
return
}
func (self *term) errIncompatibleTypes(leftValue, rightValue any) error {
leftType := getTypeName(leftValue)
leftType := typeName(leftValue)
leftText := getFormatted(leftValue, Truncate)
rightType := getTypeName(rightValue)
rightType := typeName(rightValue)
rightText := getFormatted(rightValue, Truncate)
return self.tk.Errorf(
"left operand '%s' [%s] and right operand '%s' [%s] are not compatible with operator %q",
@ -175,8 +175,8 @@ func (self *term) errIncompatibleTypes(leftValue, rightValue any) error {
func (self *term) errIncompatibleType(value any) error {
return self.tk.Errorf(
"prefix/postfix operator %q do not support operand '%v' [%T]",
self.source(), value, value)
"prefix/postfix operator %q do not support operand '%v' [%s]",
self.source(), value, typeName(value))
}
func (self *term) Errorf(template string, args ...any) (err error) {