From fda56594872e1690554cea6d4ffc0e308dd9dc26 Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Tue, 7 Jun 2016 12:29:15 -0600 Subject: [PATCH] update error rewrite to work with new type name --- CHANGELOG.md | 11 +++++++++++ tick/eval.go | 15 +++++++++------ tick/eval_test.go | 26 +++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f641cf2da..770665e5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## v1.0.0-beta2 [unreleased] + +### Release Notes + +### Features + +### Bugfixes + +- [#621](https://github.com/influxdata/kapacitor/pull/621): Fix obscure error about single vs double quotes. + + ## v1.0.0-beta1 [2016-06-06] ### Release Notes diff --git a/tick/eval.go b/tick/eval.go index bbf26876f..779fc32b9 100644 --- a/tick/eval.go +++ b/tick/eval.go @@ -239,7 +239,10 @@ func eval(n ast.Node, scope *stateful.Scope, stck *stack, predefinedVars, defaul ret := stck.Pop() if f, ok := ret.(unboundFunc); ok { // Call global function - f(nil) + _, err := f(nil) + if err != nil { + return err + } } } } @@ -483,9 +486,9 @@ func evalFunc(f *ast.FunctionNode, scope *stateful.Scope, stck *stack, args []in rec := func(obj interface{}, errp *error) { e := recover() if e != nil { - *errp = fmt.Errorf("line %d char%d: error calling func %q on obj %T: %v", f.Line(), f.Char(), f.Func, obj, e) - if strings.Contains((*errp).Error(), "*tick.ReferenceNode") && strings.Contains((*errp).Error(), "type string") { - *errp = fmt.Errorf("line %d char%d: cannot assign *tick.ReferenceNode to type string, did you use double quotes instead of single quotes?", f.Line(), f.Char()) + *errp = fmt.Errorf("line %d char %d: error calling func %q on obj %T: %v", f.Line(), f.Char(), f.Func, obj, e) + if strings.Contains((*errp).Error(), "*ast.ReferenceNode") && strings.Contains((*errp).Error(), "type string") { + *errp = fmt.Errorf("line %d char %d: cannot assign *ast.ReferenceNode to type string, did you use double quotes instead of single quotes?", f.Line(), f.Char()) } } @@ -496,12 +499,12 @@ func evalFunc(f *ast.FunctionNode, scope *stateful.Scope, stck *stack, args []in if f.Type == ast.GlobalFunc { if obj != nil { - return nil, fmt.Errorf("line %d char%d: calling global function on object %T", f.Line(), f.Char(), obj) + return nil, fmt.Errorf("line %d char %d: calling global function on object %T", f.Line(), f.Char(), obj) } // Object is nil, check for func in scope fnc, _ := scope.Get(f.Func) if fnc == nil { - return nil, fmt.Errorf("line %d char%d: no global function %q defined", f.Line(), f.Char(), f.Func) + return nil, fmt.Errorf("line %d char %d: no global function %q defined", f.Line(), f.Char(), f.Func) } method := reflect.ValueOf(fnc) o, err := callMethodReflection(method, args) diff --git a/tick/eval_test.go b/tick/eval_test.go index e188b823f..5227fca9a 100644 --- a/tick/eval_test.go +++ b/tick/eval_test.go @@ -1,6 +1,7 @@ package tick_test import ( + "errors" "fmt" "reflect" "regexp" @@ -807,27 +808,46 @@ f(strList) ` called := false - f := func(a, b, c string) { + f := func(a, b, c string) interface{} { called = true got := []string{a, b, c} exp := []string{"host", "dc", "service"} if !reflect.DeepEqual(got, exp) { t.Errorf("unexpected func args got %v exp %v", got, exp) } + return nil } scope := stateful.NewScope() scope.Set("f", f) - vars, err := tick.Evaluate(script, scope, nil, false) + _, err := tick.Evaluate(script, scope, nil, false) if err != nil { t.Fatal(err) } - t.Log(vars) if !called { t.Fatal("expected function to be called") } } +func TestEvaluate_StringQuotesError(t *testing.T) { + script := ` +f("asdf") +` + + f := func(got string) (interface{}, error) { + return nil, errors.New("function should not be called") + } + scope := stateful.NewScope() + scope.Set("f", f) + + _, err := tick.Evaluate(script, scope, nil, false) + if err == nil { + t.Fatal("expected error from invalid string call") + } else if got, exp := err.Error(), "line 2 char 1: cannot assign *ast.ReferenceNode to type string, did you use double quotes instead of single quotes?"; got != exp { + t.Errorf("unexpected error string: \ngot\n%s\nexp\n%s\n", got, exp) + } +} + //------------------------------------ // Types for TestReflectionDescriber //