1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-27 06:44:42 +01:00

Merge pull request #24 from Proximaio/feature/filter_errors_returned_as_errors

Return errors applying filters as render errors
This commit is contained in:
Oliver Steele 2017-07-21 11:27:54 -04:00 committed by GitHub
commit a5a8785a7d
4 changed files with 21 additions and 1 deletions

View File

@ -22,7 +22,10 @@ func makeFilter(fn valueFn, name string, args []valueFn) valueFn {
return func(ctx Context) evaluator.Value { return func(ctx Context) evaluator.Value {
result, err := ctx.ApplyFilter(name, fn, args) result, err := ctx.ApplyFilter(name, fn, args)
if err != nil { if err != nil {
panic(err) panic(FilterError{
FilterName: name,
Err: err,
})
} }
return evaluator.ValueOf(result) return evaluator.ValueOf(result)
} }

View File

@ -53,6 +53,8 @@ func (e expression) Evaluate(ctx Context) (out interface{}, err error) {
err = e err = e
case UndefinedFilter: case UndefinedFilter:
err = e err = e
case FilterError:
err = e
default: default:
panic(r) panic(r)
} }

View File

@ -1,6 +1,7 @@
package expressions package expressions
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -134,6 +135,10 @@ func TestEvaluateString(t *testing.T) {
_, err = EvaluateString("1 | undefined_filter", ctx) _, err = EvaluateString("1 | undefined_filter", ctx)
require.Error(t, err) 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) { func TestClosure(t *testing.T) {

View File

@ -21,6 +21,16 @@ func (e UndefinedFilter) Error() string {
return fmt.Sprintf("undefined filter %q", string(e)) 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 type valueFn func(Context) evaluator.Value
// AddFilter adds a filter to the filter dictionary. // AddFilter adds a filter to the filter dictionary.