Skip to content

Commit

Permalink
fix: catch panic in try catch (#455)
Browse files Browse the repository at this point in the history
Ensure that a panic is caught if thrown in a try catch block.

Fixes #382
  • Loading branch information
stevenh authored Nov 26, 2022
1 parent 08e7a8d commit 52d4954
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 15 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,18 @@ func Test_issue386(t *testing.T) {
})
}
}

func Test_issue382(t *testing.T) {
vm := New()
vm.Set("panicFunc", func(call FunctionCall) Value {
panic("test")
})
_, err := vm.Run(`
try {
panicFunc()
} catch (err) {
console.log("panic triggered:", err)
}
`)
require.NoError(t, err)
}
8 changes: 4 additions & 4 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (self *_runtime) tryCatchEvaluate(inner func() Value) (tryValue Value, exce
// throw = Something was thrown
// throwValue = The value of what was thrown
// other = Something that changes flow (return, break, continue) that is not a throw
// Otherwise, some sort of unknown panic happened, we'll just propagate it
// Otherwise, some sort of unknown panic happened, we'll just propagate it.
defer func() {
if caught := recover(); caught != nil {
if exception, ok := caught.(*_exception); ok {
Expand All @@ -135,13 +135,13 @@ func (self *_runtime) tryCatchEvaluate(inner func() Value) (tryValue Value, exce
exception = true
tryValue = caught
default:
panic(caught)
exception = true
tryValue = toValue(caught)
}
}
}()

tryValue = inner()
return
return inner(), false
}

// toObject
Expand Down

0 comments on commit 52d4954

Please sign in to comment.