CUE has its own error type which implements Go’s error interface.
We can use the cue/errors package in order to unpack and print them.
errors.go
packagemainimport("fmt""cuelang.org/go/cue""cuelang.org/go/cue/cuecontext""cuelang.org/go/cue/errors")constschema=`
#schema: {
i: int
s: string
}
`constval=`
v: #schema & {
i: "hello"
s: 1
}
`funcmain(){c:=cuecontext.New()s:=c.CompileString(schema,cue.Filename("schema.cue"))v:=c.CompileString(val,cue.Scope(s),cue.Filename("val.cue"))// check for errors during compilingifv.Err()!=nil{msg:=errors.Details(v.Err(),nil)fmt.Printf("Compile Error:\n%s\n",msg)}// To get all errors, we need to validateerr:=v.Validate()iferr!=nil{msg:=errors.Details(err,nil)fmt.Printf("Validate Error:\n%s\n",msg)}}
go run errors.go
Compile Error:
v.i: conflicting values int and "hello" (mismatched types int and string):
schema.cue:3:5
val.cue:3:5
Validate Error:
v.i: conflicting values int and "hello" (mismatched types int and string):
schema.cue:3:5
val.cue:3:5
v.s: conflicting values string and 1 (mismatched types string and int):
schema.cue:4:5
val.cue:4:5