Skip to content

Commit

Permalink
Merge pull request #183 from grafana/lowfruit-validation
Browse files Browse the repository at this point in the history
First pass on refining error messages
  • Loading branch information
sam boyer committed Jul 13, 2023
2 parents 375c1b4 + bf60390 commit d094f49
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"

terrors "github.com/grafana/thema/errors"
)

Expand Down Expand Up @@ -143,6 +144,23 @@ func mungeValidateErr(err error, sch Schema) error {

errs = append(errs, err)
continue
case 2:
dataval, dvok := vals[0].(string)
schval, svok := vals[1].(string)
if !svok || !dvok {
break
}

errs = append(errs, &twosidederr{
schpos: schpos,
datapos: datapos,
coords: x,
sv: schval,
dv: dataval,
code: terrors.OutOfBounds,
})
continue

case 4:
schval, svok := vals[0].(string)
dataval, dvok := vals[1].(string)
Expand All @@ -152,6 +170,12 @@ func mungeValidateErr(err error, sch Schema) error {
break
}

if schkind&cue.NumberKind > 0 {
if m, ok := schErrMsgFormatMap[schval]; ok {
schval = m
}
}

err := &twosidederr{
schpos: schpos,
datapos: datapos,
Expand All @@ -168,13 +192,17 @@ func mungeValidateErr(err error, sch Schema) error {
errs = append(errs, err)
continue
}

// We missed a case, wrap CUE err in a plea for help
errs = append(errs, fmt.Errorf("no Thema handler for CUE error, please file an issue against github.com/grafana/thema\nto improve this error output!\n\n%w", ee))
}
return errs
}

var schErrMsgFormatMap = map[string]string{
"int & >=-2147483648 & <=2147483647": "int32",
"int & >=-9223372036854775808 & <=9223372036854775807": "int64",
">=-340282346638528859811704183484516925440 & <=340282346638528859811704183484516925440": "float32",
">=-1.797693134862315708145274237317043567981E+308 & <=1.797693134862315708145274237317043567981E+308": "float64",
}

func splitTokens(poslist []token.Pos) (schpos, datapos []token.Pos) {
if len(poslist) == 0 {
return
Expand All @@ -196,8 +224,8 @@ func splitTokens(poslist []token.Pos) (schpos, datapos []token.Pos) {

func trimThemaPath(parts []string) []string {
for i, s := range parts {
if s == "seqs" {
return parts[i+4:]
if s == "schemas" {
return parts[i+3:]
}
}

Expand Down

0 comments on commit d094f49

Please sign in to comment.