mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Add Wrap and Unwrap method to hold self error (#352)
This commit is contained in:
+1
-1
@@ -1348,7 +1348,7 @@ func TestCompile_exposed_error(t *testing.T) {
|
||||
|
||||
b, err := json.Marshal(err)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, `{"Line":1,"Column":2,"Message":"invalid operation: == (mismatched types int and bool)","Snippet":"\n | 1 == true\n | ..^"}`, string(b))
|
||||
require.Equal(t, `{"Line":1,"Column":2,"Message":"invalid operation: == (mismatched types int and bool)","Snippet":"\n | 1 == true\n | ..^","Prev":null}`, string(b))
|
||||
}
|
||||
|
||||
func TestCompile_deref(t *testing.T) {
|
||||
|
||||
@@ -10,6 +10,7 @@ type Error struct {
|
||||
Location
|
||||
Message string
|
||||
Snippet string
|
||||
Prev error
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
@@ -44,6 +45,16 @@ func (e *Error) Bind(source *Source) *Error {
|
||||
return e
|
||||
}
|
||||
|
||||
|
||||
func (e *Error) Unwrap() error {
|
||||
return e.Prev
|
||||
}
|
||||
|
||||
func (e *Error) Wrap(err error) {
|
||||
e.Prev = err
|
||||
}
|
||||
|
||||
|
||||
func (e *Error) format() string {
|
||||
if e.Location.Empty() {
|
||||
return e.Message
|
||||
|
||||
@@ -61,6 +61,9 @@ func (vm *VM) Run(program *Program, env interface{}) (_ interface{}, err error)
|
||||
Location: program.Locations[vm.ip-1],
|
||||
Message: fmt.Sprintf("%v", r),
|
||||
}
|
||||
if err, ok := r.(error); ok {
|
||||
f.Wrap(err)
|
||||
}
|
||||
err = f.Bind(program.Source)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -274,6 +274,10 @@ func TestRun_MethodWithError(t *testing.T) {
|
||||
out, err := vm.Run(program, env)
|
||||
require.EqualError(t, err, "error (1:1)\n | WillError(\"yes\")\n | ^")
|
||||
require.Equal(t, nil, out)
|
||||
|
||||
selfErr := errors.Unwrap(err)
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "error", selfErr.Error())
|
||||
}
|
||||
|
||||
func TestRun_FastMethods(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user