Skip to content

Commit

Permalink
update error rewrite to work with new type name
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Jun 7, 2016
1 parent c6f7db5 commit c0da343
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
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
22 changes: 20 additions & 2 deletions tick/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,16 +818,34 @@ f(strList)
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) {
t.Fatal("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

0 comments on commit c0da343

Please sign in to comment.