Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cel/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (e *Env) Check(ast *Ast) (*Ast, *Issues) {
chk, err := e.initChecker()
if err != nil {
errs := common.NewErrors(ast.Source())
errs.ReportError(common.NoLocation, err.Error())
Comment thread
TristonianJones marked this conversation as resolved.
errs.ReportErrorString(common.NoLocation, err.Error())
return nil, NewIssuesWithSourceInfo(errs, ast.NativeRep().SourceInfo())
}

Expand Down
20 changes: 10 additions & 10 deletions cel/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,15 +731,15 @@ var (
func timestampGetFullYear(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.Year())
}

func timestampGetMonth(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
// CEL spec indicates that the month should be 0-based, but the Time value
// for Month() is 1-based.
Expand All @@ -749,63 +749,63 @@ func timestampGetMonth(ts, tz ref.Val) ref.Val {
func timestampGetDayOfYear(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.YearDay() - 1)
}

func timestampGetDayOfMonthZeroBased(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.Day() - 1)
}

func timestampGetDayOfMonthOneBased(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.Day())
}

func timestampGetDayOfWeek(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.Weekday())
}

func timestampGetHours(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.Hour())
}

func timestampGetMinutes(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.Minute())
}

func timestampGetSeconds(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.Second())
}

func timestampGetMilliseconds(ts, tz ref.Val) ref.Val {
t, err := inTimeZone(ts, tz)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(t.Nanosecond() / 1000000)
}
Expand Down
5 changes: 5 additions & 0 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (e *Errors) ReportError(l Location, format string, args ...any) {
e.ReportErrorAtID(0, l, format, args...)
}

// ReportErrorString records an error at a source location.
func (e *Errors) ReportErrorString(l Location, message string) {
e.ReportErrorAtID(0, l, "%s", message)
}

// ReportErrorAtID records an error at a source location and expression id.
func (e *Errors) ReportErrorAtID(id int64, l Location, format string, args ...any) {
e.numErrors++
Expand Down
6 changes: 6 additions & 0 deletions common/types/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func NewErr(format string, args ...any) ref.Val {
return &Err{error: fmt.Errorf(format, args...)}
}

// NewErr creates a new Err with the provided message.
// TODO: Audit the use of this function and standardize the error messages and codes.
func NewErrFromString(message string) ref.Val {
return &Err{error: errors.New(message)}
}

// NewErrWithNodeID creates a new Err described by the format string and args.
// TODO: Audit the use of this function and standardize the error messages and codes.
func NewErrWithNodeID(id int64, format string, args ...any) ref.Val {
Expand Down
4 changes: 2 additions & 2 deletions common/types/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (l *baseList) Equal(other ref.Val) ref.Val {
func (l *baseList) Get(index ref.Val) ref.Val {
ind, err := IndexOrError(index)
if err != nil {
return ValOrErr(index, err.Error())
return ValOrErr(index, "%v", err)
}
if ind < 0 || ind >= l.size {
return NewErr("index '%d' out of range in list size '%d'", ind, l.Size())
Expand Down Expand Up @@ -427,7 +427,7 @@ func (l *concatList) Equal(other ref.Val) ref.Val {
func (l *concatList) Get(index ref.Val) ref.Val {
ind, err := IndexOrError(index)
if err != nil {
return ValOrErr(index, err.Error())
return ValOrErr(index, "%v", err)
}
i := Int(ind)
if i < l.prevList.Size().(Int) {
Expand Down
2 changes: 1 addition & 1 deletion common/types/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (o *protoObj) Get(index ref.Val) ref.Val {
}
fv, err := fd.GetFrom(o.value)
if err != nil {
return NewErr(err.Error())
return NewErrFromString(err.Error())
}
return o.NativeToValue(fv)
}
Expand Down
2 changes: 1 addition & 1 deletion ext/formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ func (stringFormatValidator) Validate(env *cel.Env, _ cel.ValidatorConfig, a *as
// use a placeholder locale, since locale doesn't affect syntax
_, err := parseFormatString(formatStr, formatCheck, formatCheck, "en_US")
if err != nil {
iss.ReportErrorAtID(getErrorExprID(e.ID(), err), err.Error())
iss.ReportErrorAtID(getErrorExprID(e.ID(), err), "%v", err)
continue
}
seenArgs := formatCheck.argsRequested
Expand Down
8 changes: 4 additions & 4 deletions ext/guards.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ import (

func intOrError(i int64, err error) ref.Val {
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Int(i)
}

func bytesOrError(bytes []byte, err error) ref.Val {
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.Bytes(bytes)
}

func stringOrError(str string, err error) ref.Val {
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.String(str)
}

func listStringOrError(strs []string, err error) ref.Val {
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return types.DefaultTypeAdapter.NativeToValue(strs)
}
Expand Down
4 changes: 2 additions & 2 deletions ext/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (tp *nativeTypeProvider) NewValue(typeName string, fields map[string]ref.Va
}
fieldVal, err := val.ConvertToNative(refFieldDef.Type)
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
refField := refVal.FieldByIndex(refFieldDef.Index)
refFieldVal := reflect.ValueOf(fieldVal)
Expand Down Expand Up @@ -450,7 +450,7 @@ func convertToCelType(refType reflect.Type) (*cel.Type, bool) {
func (tp *nativeTypeProvider) newNativeObject(val any, refValue reflect.Value) ref.Val {
valType, err := newNativeType(tp.options.fieldNameHandler, refValue.Type())
if err != nil {
return types.NewErr(err.Error())
return types.NewErrFromString(err.Error())
}
return &nativeObj{
Adapter: tp,
Expand Down
4 changes: 2 additions & 2 deletions interpreter/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ func TestAttributeStateTracking(t *testing.T) {
}
parsed, errors := p.Parse(src)
if len(errors.GetErrors()) != 0 {
t.Fatalf(errors.ToDisplayString())
t.Fatal(errors.ToDisplayString())
}
cont := containers.DefaultContainer
reg := newTestRegistry(t)
Expand All @@ -1135,7 +1135,7 @@ func TestAttributeStateTracking(t *testing.T) {
}
checked, errors := checker.Check(parsed, src, env)
if len(errors.GetErrors()) != 0 {
t.Fatalf(errors.ToDisplayString())
t.Fatal(errors.ToDisplayString())
}
in, err := NewActivation(tc.in)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,7 @@ func TestInterpreter_SetProto2PrimitiveFields(t *testing.T) {
types.NewObjectType("google.expr.proto2.test.TestAllTypes")))
checked, errors := checker.Check(parsed, src, env)
if len(errors.GetErrors()) != 0 {
t.Errorf(errors.ToDisplayString())
t.Error(errors.ToDisplayString())
}

attrs := NewAttributeFactory(cont, reg, reg)
Expand Down Expand Up @@ -1829,7 +1829,7 @@ func TestInterpreter_MissingIdentInSelect(t *testing.T) {
env.AddIdents(decls.NewVariable("a.b", types.DynType))
checked, errors := checker.Check(parsed, src, env)
if len(errors.GetErrors()) != 0 {
t.Fatalf(errors.ToDisplayString())
t.Fatal(errors.ToDisplayString())
}

attrs := NewPartialAttributeFactory(cont, reg, reg)
Expand Down Expand Up @@ -1885,7 +1885,7 @@ func TestInterpreter_TypeConversionOpt(t *testing.T) {
env := newTestEnv(t, cont, reg)
checked, errors := checker.Check(parsed, src, env)
if len(errors.GetErrors()) != 0 {
t.Fatalf(errors.ToDisplayString())
t.Fatal(errors.ToDisplayString())
}
attrs := NewAttributeFactory(cont, reg, reg)
interp := newStandardInterpreter(t, cont, reg, reg, attrs)
Expand Down
6 changes: 2 additions & 4 deletions parser/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package parser

import (
"fmt"

"github.com/google/cel-go/common"
)

Expand All @@ -31,11 +29,11 @@ func (e *parseErrors) errorCount() int {
}

func (e *parseErrors) internalError(message string) {
e.errs.ReportErrorAtID(0, common.NoLocation, message)
e.errs.ReportErrorAtID(0, common.NoLocation, "%s", message)
}

func (e *parseErrors) syntaxError(l common.Location, message string) {
e.errs.ReportErrorAtID(0, l, fmt.Sprintf("Syntax error: %s", message))
e.errs.ReportErrorAtID(0, l, "Syntax error: %s", message)
}

func (e *parseErrors) reportErrorAtID(id int64, l common.Location, message string, args ...any) {
Expand Down
2 changes: 1 addition & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func (p *parser) expandMacro(exprID int64, function string, target ast.Expr, arg
loc = p.helper.getLocation(exprID)
}
p.helper.deleteID(exprID)
return p.reportError(loc, err.Message), true
return p.reportError(loc, "%s", err.Message), true
}
// A nil value from the macro indicates that the macro implementation decided that
// an expansion should not be performed.
Expand Down
2 changes: 1 addition & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,7 @@ func TestParse(t *testing.T) {
if tc.E == "" {
t.Fatalf("Unexpected errors: %v", actualErr)
} else if !test.Compare(actualErr, tc.E) {
t.Fatalf(test.DiffMessage("Error mismatch", actualErr, tc.E))
t.Fatal(test.DiffMessage("Error mismatch", actualErr, tc.E))
}
return
} else if tc.E != "" {
Expand Down
2 changes: 1 addition & 1 deletion repl/typefmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func ParseType(t string) (*exprpb.Type, error) {
for i, e := range errs {
msgs[i] = e.Error()
}
err = fmt.Errorf("errors parsing type:\n" + strings.Join(msgs, "\n"))
err = fmt.Errorf("errors parsing type:\n%s", strings.Join(msgs, "\n"))
}

return result, err
Expand Down