Skip to content

Commit

Permalink
Merge pull request #24 from Proximaio/feature/filter_errors_returned_…
Browse files Browse the repository at this point in the history
…as_errors

Return errors applying filters as render errors
  • Loading branch information
osteele authored Jul 21, 2017
2 parents de5fffa + 3d99b41 commit a5a8785
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion expressions/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ func makeFilter(fn valueFn, name string, args []valueFn) valueFn {
return func(ctx Context) evaluator.Value {
result, err := ctx.ApplyFilter(name, fn, args)
if err != nil {
panic(err)
panic(FilterError{
FilterName: name,
Err: err,
})
}
return evaluator.ValueOf(result)
}
Expand Down
2 changes: 2 additions & 0 deletions expressions/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func (e expression) Evaluate(ctx Context) (out interface{}, err error) {
err = e
case UndefinedFilter:
err = e
case FilterError:
err = e
default:
panic(r)
}
Expand Down
5 changes: 5 additions & 0 deletions expressions/expressions_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package expressions

import (
"errors"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -134,6 +135,10 @@ func TestEvaluateString(t *testing.T) {

_, err = EvaluateString("1 | undefined_filter", ctx)
require.Error(t, err)

cfg.AddFilter("error", func(input interface{}) (string, error) { return "", errors.New("test error") })
_, err = EvaluateString("1 | error", ctx)
require.Error(t, err)
}

func TestClosure(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions expressions/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ func (e UndefinedFilter) Error() string {
return fmt.Sprintf("undefined filter %q", string(e))
}

// FilterError is the error returned by a filter when it is applied
type FilterError struct {
FilterName string
Err error
}

func (e FilterError) Error() string {
return fmt.Sprintf("error applying filter %q (%q)", e.FilterName, e.Err)
}

type valueFn func(Context) evaluator.Value

// AddFilter adds a filter to the filter dictionary.
Expand Down

0 comments on commit a5a8785

Please sign in to comment.