Skip to content

Commit

Permalink
Panic earlier if context is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 11, 2024
1 parent ff64e13 commit 53cb049
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion dbutil/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ func (db *Database) QueryRow(ctx context.Context, query string, args ...any) *sq
}

func (db *Database) BeginTx(ctx context.Context, opts *sql.TxOptions) (*LoggingTxn, error) {
if ctx == nil {
panic("BeginTx() called with nil ctx")
}
return db.LoggingDB.BeginTx(ctx, opts)
}

func (db *Database) DoTxn(ctx context.Context, opts *sql.TxOptions, fn func(ctx context.Context) error) error {
if ctx == nil {
panic("DoTxn() called with nil ctx")
}
if ctx.Value(ContextKeyDatabaseTransaction) != nil {
zerolog.Ctx(ctx).Trace().Msg("Already in a transaction, not creating a new one")
return fn(ctx)
Expand Down Expand Up @@ -123,7 +129,7 @@ func (db *Database) DoTxn(ctx context.Context, opts *sql.TxOptions, fn func(ctx

func (db *Database) Conn(ctx context.Context) Execable {
if ctx == nil {
return &db.LoggingDB
panic("Conn() called with nil ctx")
}
txn, ok := ctx.Value(ContextKeyDatabaseTransaction).(Transaction)
if ok {
Expand Down

0 comments on commit 53cb049

Please sign in to comment.