From d16d9cab48b31fbe2c000f8a8f121606ca2e6229 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 16 Jul 2016 14:44:28 +0100 Subject: [PATCH] Enable catching panics in database calls --- db/db.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/db/db.go b/db/db.go index e404479f..f9d29b45 100644 --- a/db/db.go +++ b/db/db.go @@ -82,8 +82,13 @@ func Execute(ctx *fibre.Context, txt interface{}) (out []interface{}, err error) go execute(ctx, ast, chn) - for res := range chn { - out = append(out, res) + for msg := range chn { + switch res := msg.(type) { + case error: + return nil, res + default: + out = append(out, res) + } } 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 {