Skip to content

Commit 6d23853

Browse files
committed
render error
1 parent e831d0e commit 6d23853

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

provider/parameter.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"regexp"
1212
"strconv"
13+
"strings"
1314

1415
"github.com/google/uuid"
1516
"github.com/hashicorp/go-cty/cty"
@@ -437,13 +438,13 @@ func (v *Validation) Valid(typ, value string) error {
437438
case "number":
438439
num, err := strconv.Atoi(value)
439440
if err != nil {
440-
return validationError(v.Error, "value %q is not a number", value)
441+
return takeFirstError(v.errorRendered(value), fmt.Errorf("value %q is not a number", value))
441442
}
442443
if !v.MinDisabled && num < v.Min {
443-
return validationError(v.Error, "value %d is less than the minimum %d", num, v.Min)
444+
return takeFirstError(v.errorRendered(value), fmt.Errorf("value %d is less than the minimum %d", num, v.Min))
444445
}
445446
if !v.MaxDisabled && num > v.Max {
446-
return validationError(v.Error, "value %d is more than the maximum %d", num, v.Max)
447+
return takeFirstError(v.errorRendered(value), fmt.Errorf("value %d is more than the maximum %d", num, v.Max))
447448
}
448449
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
449450
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
@@ -466,9 +467,22 @@ func ParameterEnvironmentVariable(name string) string {
466467
return "CODER_PARAMETER_" + hex.EncodeToString(sum[:])
467468
}
468469

469-
func validationError(customMessage, defaultMessage string, a ...any) error {
470-
if customMessage != "" {
471-
return fmt.Errorf(customMessage)
470+
func takeFirstError(errs ...error) error {
471+
for _, err := range errs {
472+
if err != nil {
473+
return err
474+
}
475+
}
476+
return xerrors.Errorf("developer error: error message is not provided")
477+
}
478+
479+
func (v *Validation) errorRendered(value string) error {
480+
if v.Error == "" {
481+
return nil
472482
}
473-
return fmt.Errorf(defaultMessage, a...)
483+
r := strings.NewReplacer(
484+
"{min}", fmt.Sprintf("%d", v.Min),
485+
"{max}", fmt.Sprintf("%d", v.Max),
486+
"{value}", value)
487+
return xerrors.Errorf(r.Replace(v.Error))
474488
}

0 commit comments

Comments
 (0)