Skip to content

Commit

Permalink
BAAS-26309: Only use json stringify for arrays and objects (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcannon91 authored Jan 18, 2024
1 parent b6ea3a7 commit a7b4406
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
21 changes: 15 additions & 6 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 28 additions & 1 deletion runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2998,7 +2998,7 @@ func TestExceptionStringifyError(t *testing.T) {
exceptionVal: func() Value {
return newStringValue("hello")
},
expectedString: "\"hello\"",
expectedString: "hello",
},
{
description: "Null",
Expand All @@ -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(),
Expand Down

0 comments on commit a7b4406

Please sign in to comment.