Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAAS-26308: Add StringifyError to Exception struct #108

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@
return e.nativeErr
}

func (e *Exception) StringifyError(r *Runtime) string {
val := r.builtinJSON_stringify(FunctionCall{
Arguments: []Value{
e.val,
},
})

return val.String()
}

type uncatchableException struct {
err error
}
Expand Down Expand Up @@ -470,7 +480,7 @@
}
r.vm.init()

r.SetRateLimiter(rate.NewLimiter(rate.Inf, maxInt))

Check failure on line 483 in runtime.go

View workflow job for this annotation

GitHub Actions / test (1.16.x, ubuntu-latest, 386)

constant 9007199254740992 overflows int
}

func (r *Runtime) typeErrorResult(throw bool, args ...interface{}) {
Expand Down
87 changes: 87 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2971,6 +2971,93 @@ func TestExceptionWithinAppliedObjectFunc(t *testing.T) {
}
}

func TestExceptionStringifyError(t *testing.T) {
runtime := New()

for _, tc := range []struct {
description string
exceptionVal func() Value
expectedString string
}{
{
description: "Bool",
exceptionVal: func() Value {
return valueBool(true)
},
expectedString: "true",
},
{
description: "Int",
exceptionVal: func() Value {
return valueInt(1234)
},
expectedString: "1234",
},
{
description: "String",
exceptionVal: func() Value {
return newStringValue("hello")
},
expectedString: "\"hello\"",
},
{
description: "Null",
exceptionVal: func() Value {
return valueNull{}
},
expectedString: "null",
},
{
description: "Undefined",
exceptionVal: func() Value {
return valueUndefined{}
},
expectedString: "undefined",
},
} {
ex := Exception{
val: tc.exceptionVal(),
}

s := ex.StringifyError(runtime)

t.Run(fmt.Sprintf("Exception Value is %s", tc.description), func(t *testing.T) {
if s != tc.expectedString {
t.Fatalf("Stringified exception did not match. Actual: %s Expected: %s", s, tc.expectedString)
}
})
}

}

func TestExceptionStringifyErrorObject(t *testing.T) {
runtime := New()

expectedData := map[string]string{
"foo": "bar",
"fizz": "buzz",
}

testObject := runtime.NewObject()

for k := range expectedData {
testObject.Set(k, expectedData[k])
}

ex := Exception{
val: testObject,
}

s := ex.StringifyError(runtime)

for expectedKey := range expectedData {
expectedSubstring := fmt.Sprintf("\"%s\":\"%s\"", expectedKey, expectedData[expectedKey])
if !strings.Contains(s, expectedSubstring) {
t.Fatalf("Stringified exception object did not contain expected substring: %s", expectedSubstring)
}
}
}

// TODO(REALMC-10739) return the actual stack trace
func TestErrorCaptureStackTrace(t *testing.T) {
const SCRIPT = `
Expand Down
Loading