1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 09:59:02 +01:00

Catch unimplemented panics

This commit is contained in:
Oliver Steele 2017-06-27 16:11:32 -04:00
parent d849e74d1d
commit b1cf056e36
4 changed files with 26 additions and 5 deletions

View File

@ -1,6 +1,20 @@
package expressions
func (e expression) Evaluate(ctx Context) (interface{}, error) {
import "github.com/osteele/liquid/errors"
func (e expression) Evaluate(ctx Context) (out interface{}, err error) {
defer func() {
if r := recover(); r != nil {
switch e := r.(type) {
case UnimplementedError:
err = e
case errors.UndefinedFilter:
err = e
default:
panic(r)
}
}
}()
return e.evaluator(ctx), nil
}

View File

@ -3,7 +3,6 @@ package expressions
import (
"fmt"
"reflect"
"runtime/debug"
"github.com/osteele/liquid/errors"
"github.com/osteele/liquid/generics"
@ -49,7 +48,7 @@ func makeFilter(f valueFn, name string, param valueFn) valueFn {
case generics.GenericError:
panic(InterpreterError(e.Error()))
default:
fmt.Println(string(debug.Stack()))
// fmt.Println(string(debug.Stack()))
panic(e)
}
}

View File

@ -21,6 +21,12 @@ type ParseError string
func (e ParseError) Error() string { return string(e) }
type UnimplementedError string
func (e UnimplementedError) Error() string {
return fmt.Sprintf("unimplemented %s", string(e))
}
// Expression is a parsed expression.
type Expression interface {
// Evaluate evaluates an expression in a context.
@ -38,6 +44,8 @@ func Parse(source string) (expr Expression, err error) {
switch e := r.(type) {
case ParseError:
err = e
case UnimplementedError:
err = e
case errors.UndefinedFilter:
err = e
default:

View File

@ -24,13 +24,13 @@ func DefineStandardFilters() {
// dates
expressions.DefineFilter("date", func(in, format interface{}) interface{} {
if format != nil {
panic("date conversion format is not implemented")
panic(expressions.UnimplementedError("date conversion format"))
}
switch in := in.(type) {
case time.Time:
return in.Format("Mon, Jan 2, 06")
default:
panic("unimplemented date conversion")
panic(expressions.UnimplementedError("date conversion"))
}
})