From 5245495ae50a3ac8c1f2b23030c193aceef5d1c4 Mon Sep 17 00:00:00 2001 From: Evans Owamoyo Date: Mon, 14 Aug 2023 12:04:54 +0500 Subject: [PATCH] wrapped validation errors with `ErrInvalidExpression` --- expression/expression.go | 12 ++++++++---- expression/expression_test.go | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/expression/expression.go b/expression/expression.go index f8830900d..6d7fdfec8 100644 --- a/expression/expression.go +++ b/expression/expression.go @@ -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{}{ @@ -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 diff --git a/expression/expression_test.go b/expression/expression_test.go index 857d546fa..498768510 100644 --- a/expression/expression_test.go +++ b/expression/expression_test.go @@ -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"