expr/common-errors.go

56 lines
1.7 KiB
Go
Raw Normal View History

2024-05-01 07:09:01 +02:00
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// common-errors.go
package expr
import (
"fmt"
)
func errTooFewParams(funcName string, minArgs, maxArgs, argCount int) (err error) {
if maxArgs < 0 {
err = fmt.Errorf("%s(): too few params -- expected %d or more, got %d", funcName, minArgs, argCount)
} else {
err = fmt.Errorf("%s(): too few params -- expected %d, got %d", funcName, minArgs, argCount)
}
return
}
func errTooMuchParams(funcName string, maxArgs, argCount int) (err error) {
err = fmt.Errorf("%s(): too much params -- expected %d, got %d", funcName, maxArgs, argCount)
return
}
2024-05-01 07:09:01 +02:00
// --- General errors
func errCantConvert(funcName string, value any, kind string) error {
2024-06-05 05:48:02 +02:00
return fmt.Errorf("%s(): can't convert %s to %s", funcName, typeName(value), kind)
2024-05-01 07:09:01 +02:00
}
func errExpectedGot(funcName string, kind string, value any) error {
2024-06-05 05:48:02 +02:00
return fmt.Errorf("%s() expected %s, got %s (%v)", funcName, kind, typeName(value), value)
2024-05-01 07:09:01 +02:00
}
func errFuncDivisionByZero(funcName string) error {
return fmt.Errorf("%s(): division by zero", funcName)
}
func errDivisionByZero() error {
return fmt.Errorf("division by zero")
}
2024-05-01 07:09:01 +02:00
// --- Parameter errors
func errMissingRequiredParameter(funcName, paramName string) error {
return fmt.Errorf("%s() missing required parameter %q", funcName, paramName)
}
func errInvalidParameterValue(funcName, paramName string, paramValue any) error {
2024-06-05 05:48:02 +02:00
return fmt.Errorf("%s() invalid value %s (%v) for parameter %q", funcName, typeName(paramValue), paramValue, paramName)
2024-05-01 07:09:01 +02:00
}
func errWrongParamType(funcName, paramName, paramType string, paramValue any) error {
2024-06-05 05:48:02 +02:00
return fmt.Errorf("%s() the %q parameter must be a %s, got a %s (%v)", funcName, paramName, paramType, typeName(paramValue), paramValue)
2024-05-01 07:09:01 +02:00
}