Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update error rewrite to work with new type ast name #621

Merged
merged 1 commit into from
Jun 7, 2016
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 9 additions & 6 deletions tick/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down Expand Up @@ -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())
}

}
Expand All @@ -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)
Expand Down
26 changes: 23 additions & 3 deletions tick/eval_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tick_test

import (
"errors"
"fmt"
"reflect"
"regexp"
Expand Down Expand Up @@ -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
//
Expand Down