mirror of
https://github.com/danog/liquid.git
synced 2024-11-30 10:19:03 +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:
commit
a5a8785a7d
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package expressions
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -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) {
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user