diff --git a/runtime.go b/runtime.go index 8b7a3995..67d69791 100644 --- a/runtime.go +++ b/runtime.go @@ -334,13 +334,22 @@ func (e *Exception) NativeError() error { } func (e *Exception) StringifyError(r *Runtime) string { - val := r.builtinJSON_stringify(FunctionCall{ - Arguments: []Value{ - e.val, - }, - }) + // if the exception has a base type of Object or Array then stringify the result + base := e.val.baseObject(r) + if base != nil { + switch base.ClassName() { + case "Array", "Object": + val := r.builtinJSON_stringify(FunctionCall{ + Arguments: []Value{ + e.val, + }, + }) + return val.String() + default: + } + } - return val.String() + return e.Error() } type uncatchableException struct { diff --git a/runtime_test.go b/runtime_test.go index 7dd4ad59..e59a4bf6 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -2998,7 +2998,7 @@ func TestExceptionStringifyError(t *testing.T) { exceptionVal: func() Value { return newStringValue("hello") }, - expectedString: "\"hello\"", + expectedString: "hello", }, { description: "Null", @@ -3014,6 +3014,33 @@ func TestExceptionStringifyError(t *testing.T) { }, expectedString: "undefined", }, + { + description: "Array", + exceptionVal: func() Value { + array := runtime.builtin_newArray([]Value{ + valueInt(1), + valueInt(2), + valueInt(3), + }, runtime.getArrayPrototype()) + + return array + }, + expectedString: "[1,2,3]", + }, + { + description: "General error", + exceptionVal: func() Value { + return runtime.newError(runtime.getError(), "i am an error") + }, + expectedString: "Error: i am an error", + }, + { + description: "TypeError", + exceptionVal: func() Value { + return runtime.NewTypeError("i am a type error") + }, + expectedString: "TypeError: i am a type error", + }, } { ex := Exception{ val: tc.exceptionVal(),