Skip to content

Commit

Permalink
wrapped validation errors with ErrInvalidExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
lordvidex committed Aug 14, 2023
1 parent c302f68 commit 5245495
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func (triggerExpression *TriggerExpression) Validate() error {
return nil
}
if triggerExpression.Expression == nil || *triggerExpression.Expression == "" {
return fmt.Errorf("trigger_type set to expression, but no expression provided")
return ErrInvalidExpression{
internalError: fmt.Errorf("trigger_type set to expression, but no expression provided"),
}
}
expression := *triggerExpression.Expression
env := map[string]interface{}{
Expand All @@ -106,12 +108,14 @@ func (triggerExpression *TriggerExpression) Validate() error {
for k, v := range triggerExpression.AdditionalTargetsValues {
env[k] = v
}
_, err := expr.Compile(
if _, err := expr.Compile(
strings.ToLower(expression),
expr.Optimize(true),
expr.Env(env),
)
return err
); err != nil {
return ErrInvalidExpression{err}
}
return nil
}

// Evaluate gets trigger expression and evaluates it for given parameters using govaluate
Expand Down
2 changes: 1 addition & 1 deletion expression/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestValidate(t *testing.T) {
})
Convey("Test bad expressions", t, func() {
err := (&TriggerExpression{Expression: nil, TriggerType: moira.ExpressionTrigger}).Validate()
So(err, ShouldResemble, fmt.Errorf("trigger_type set to expression, but no expression provided"))
So(err, ShouldResemble, ErrInvalidExpression{fmt.Errorf("trigger_type set to expression, but no expression provided")})
})
Convey("Test invalid expressions", t, func() {
expression := "t1 > 10 && t2 > 3 ? OK : ddd"
Expand Down

0 comments on commit 5245495

Please sign in to comment.