Enable catching panics in database calls

This commit is contained in:
Tobie Morgan Hitchcock 2016-07-16 14:44:28 +01:00
parent 8a0849d6da
commit d16d9cab48

View file

@ -82,8 +82,13 @@ func Execute(ctx *fibre.Context, txt interface{}) (out []interface{}, err error)
go execute(ctx, ast, chn) go execute(ctx, ast, chn)
for res := range chn { for msg := range chn {
out = append(out, res) switch res := msg.(type) {
case error:
return nil, res
default:
out = append(out, res)
}
} }
return return
@ -145,9 +150,16 @@ func detail(e error) interface{} {
} }
} }
func execute(ctx *fibre.Context, ast *sql.Query, chn chan interface{}) { func execute(ctx *fibre.Context, ast *sql.Query, chn chan<- interface{}) {
defer close(chn) defer func() {
if r := recover(); r != nil {
if err, ok := r.(error); ok {
chn <- err
}
}
close(chn)
}()
for _, s := range ast.Statements { for _, s := range ast.Statements {