Handle panics by deferring recover as an error (#345)

* Handle panics by deferring recover as an error

* Check for type in recover handler
This commit is contained in:
Mikael Ganehag Brorsson 2021-11-14 00:10:47 +01:00 committed by GitHub
parent 3b65ddf2b8
commit a7666f0e7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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()
}()