WithTransaction: dump stack if wrapped func panics

This commit is contained in:
David Robertson 2023-06-06 12:06:03 +01:00
parent 5688b0d2f7
commit f2b4898446
No known key found for this signature in database
GPG Key ID: 903ECE108A39DEDD

View File

@ -1,11 +1,21 @@
package sqlutil
import (
"context"
"fmt"
"github.com/matrix-org/sliding-sync/internal"
"github.com/rs/zerolog"
"os"
"runtime/debug"
"github.com/jmoiron/sqlx"
)
var logger = zerolog.New(os.Stdout).With().Timestamp().Logger().Output(zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: "15:04:05",
})
// WithTransaction runs a block of code passing in an SQL transaction
// If the code returns an error or panics then the transactions is rolled back
// Otherwise the transaction is committed.
@ -18,6 +28,10 @@ func WithTransaction(db *sqlx.DB, fn func(txn *sqlx.Tx) error) (err error) {
defer func() {
panicErr := recover()
if err == nil && panicErr != nil {
// TODO: thread a context through to here?
ctx := context.Background()
logger.Error().Msg(string(debug.Stack()))
internal.GetSentryHubFromContextOrDefault(ctx).RecoverWithContext(ctx, panicErr)
err = fmt.Errorf("panic: %v", panicErr)
}
var txnErr error