45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package merrors
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// InvalidArgumentClassified returns an invalid-argument error with a stable
|
|
// machine reason and structured field names for transport adapters.
|
|
func InvalidArgumentClassified(reason, msg string, argumentNames ...string) error {
|
|
return classifiedError{
|
|
base: ErrInvalidArg,
|
|
msg: invalidArgumentMessage(msg, argumentNames...),
|
|
reason: strings.TrimSpace(reason),
|
|
argumentNames: normalizeArgumentNames(argumentNames),
|
|
}
|
|
}
|
|
|
|
func invalidArgumentMessage(msg string, argumentNames ...string) string {
|
|
normalizedNames := normalizeArgumentNames(argumentNames)
|
|
names := make([]string, 0, len(normalizedNames))
|
|
for _, name := range normalizedNames {
|
|
names = append(names, fmt.Sprintf("%q", name))
|
|
}
|
|
if len(names) == 0 {
|
|
return msg
|
|
}
|
|
|
|
prefix := "broken argument"
|
|
if len(names) > 1 {
|
|
prefix = "broken arguments"
|
|
}
|
|
return fmt.Sprintf("%s %s: %s", prefix, strings.Join(names, ", "), msg)
|
|
}
|
|
|
|
func normalizeArgumentNames(argumentNames []string) []string {
|
|
names := make([]string, 0, len(argumentNames))
|
|
for _, name := range argumentNames {
|
|
if name = strings.TrimSpace(name); name != "" {
|
|
names = append(names, name)
|
|
}
|
|
}
|
|
return names
|
|
}
|