entc/gen: allow catching nested tx attemps with errors.Is (#3636)

This commit is contained in:
Ariel Mashraki
2023-07-08 12:03:37 +03:00
committed by GitHub
parent 1ec7523803
commit c46cf6317b
36 changed files with 144 additions and 36 deletions

View File

@@ -108,11 +108,14 @@ func Open(driverName, dataSourceName string, options ...Option) (*Client, error)
}
}
// ErrTxStarted is returned when trying to start a new transaction from a transactional client.
var ErrTxStarted = errors.New("ent: cannot start a transaction within a transaction")
// Tx returns a new transactional client. The provided context
// is used until the transaction is committed or rolled back.
func (c *Client) Tx(ctx context.Context) (*Tx, error) {
if _, ok := c.driver.(*txDriver); ok {
return nil, errors.New("ent: cannot start a transaction within a transaction")
return nil, ErrTxStarted
}
tx, err := newTx(ctx, c.driver)
if err != nil {