Skip to content

Commit

Permalink
Handle panics by deferring recover as an error (#345)
Browse files Browse the repository at this point in the history
* Handle panics by deferring recover as an error

* Check for type in recover handler
  • Loading branch information
ganehag authored Nov 13, 2021
1 parent 3b65ddf commit a7666f0
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ func (c *Compiled) RunContext(ctx context.Context) (err error) {
v := NewVM(c.bytecode, c.globals, c.maxAllocs)
ch := make(chan error, 1)
go func() {
defer func() {
if r := recover(); r != nil {
switch e := r.(type) {
case string:
ch <- fmt.Errorf(e)
case error:
ch <- e
default:
ch <- fmt.Errorf("unknown panic: %v", e)
}
}
}()
ch <- v.Run()
}()

Expand Down

0 comments on commit a7666f0

Please sign in to comment.