Skip to content

Commit

Permalink
Return Error When Expression Can't Be Parsed
Browse files Browse the repository at this point in the history
Update exprConverter function to return an error if the Expr expression
can't be parsed so it can be handled by the caller.
  • Loading branch information
cwarden committed Dec 13, 2024
1 parent ac79353 commit d5c9d8e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func (e *Execution) ExecuteContext(ctx context.Context) (force.JobInfo, error) {
}
}
if e.Expr != "" {
e.Converter = exprConverter(e.Expr, apexContext)
e.Converter, err = exprConverter(e.Expr, apexContext)
if err != nil {
return result.JobInfo, fmt.Errorf("Expr error: %w", err)
}
} else if e.Converter == nil {
return result.JobInfo, fmt.Errorf("Expr or Converter must be defined")
}
Expand Down
6 changes: 3 additions & 3 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ func exprFunctions() []expr.Option {
return exprFunctions
}

func exprConverter(expression string, context any) func(force.ForceRecord) []force.ForceRecord {
func exprConverter(expression string, context any) (func(force.ForceRecord) []force.ForceRecord, error) {
env := Env{
"record": force.ForceRecord{},
"apex": context,
}
program, err := expr.Compile(expression, append(exprFunctions(), expr.Env(env))...)
if err != nil {
log.Fatalln("Invalid expression:", err)
return nil, fmt.Errorf("Invalid expression: %w", err)
}
converter := func(record force.ForceRecord) []force.ForceRecord {
env := Env{
Expand All @@ -245,5 +245,5 @@ func exprConverter(expression string, context any) func(force.ForceRecord) []for
log.Warnln("Unexpected value. It should be a map or array or maps. Got", out)
return []force.ForceRecord{}
}
return converter
return converter, nil
}

0 comments on commit d5c9d8e

Please sign in to comment.