Skip to content

Commit 2793c90

Browse files
committed
fix error AlreadyHaveTxError
1 parent 29397fb commit 2793c90

File tree

5 files changed

+38
-35
lines changed

5 files changed

+38
-35
lines changed

internal/table/conn/conn.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (c *Conn) queryContext(ctx context.Context, query string, args []driver.Nam
310310
case ScriptingQueryMode:
311311
return c.execScriptingQuery(ctx, normalizedQuery, parameters)
312312
default:
313-
return nil, fmt.Errorf("unsupported query mode '%s' on Conn query", queryMode)
313+
return nil, fmt.Errorf("unsupported query mode '%s' on conn query", queryMode)
314314
}
315315
}
316316

@@ -451,11 +451,7 @@ func (c *Conn) ID() string {
451451
func (c *Conn) beginTx(ctx context.Context, txOptions driver.TxOptions) (currentTx, error) {
452452
if c.currentTx != nil {
453453
return nil, badconn.Map(
454-
xerrors.WithStackTrace(
455-
fmt.Errorf("broken Conn state: Conn=%q already have current tx=%q",
456-
c.ID(), c.currentTx.ID(),
457-
),
458-
),
454+
xerrors.WithStackTrace(xerrors.AlreadyHasTx(c.currentTx.ID())),
459455
)
460456
}
461457

internal/table/conn/errors.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,6 @@ import (
1010
var (
1111
ErrUnsupported = driver.ErrSkip
1212
errDeprecated = driver.ErrSkip
13-
errConnClosedEarly = xerrors.Retryable(errors.New("Conn closed early"), xerrors.InvalidObject())
14-
errNotReadyConn = xerrors.Retryable(errors.New("Conn not ready"), xerrors.InvalidObject())
13+
errConnClosedEarly = xerrors.Retryable(errors.New("conn closed early"), xerrors.InvalidObject())
14+
errNotReadyConn = xerrors.Retryable(errors.New("conn not ready"), xerrors.InvalidObject())
1515
)
16-
17-
type AlreadyHaveTxError struct {
18-
currentTx string
19-
}
20-
21-
func (err *AlreadyHaveTxError) Error() string {
22-
return "Conn already have an open currentTx: " + err.currentTx
23-
}
24-
25-
func (err *AlreadyHaveTxError) As(target interface{}) bool {
26-
switch t := target.(type) {
27-
case *AlreadyHaveTxError:
28-
t.currentTx = err.currentTx
29-
30-
return true
31-
default:
32-
return false
33-
}
34-
}

internal/table/conn/tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ func (tx *transaction) checkTxState() error {
5252
return nil
5353
}
5454
if tx.conn.currentTx == nil {
55-
return fmt.Errorf("broken Conn state: tx=%q not related to Conn=%q",
55+
return fmt.Errorf("broken conn state: tx=%q not related to conn=%q",
5656
tx.ID(), tx.conn.ID(),
5757
)
5858
}
5959

60-
return fmt.Errorf("broken Conn state: tx=%s not related to Conn=%q (Conn have current tx=%q)",
60+
return fmt.Errorf("broken conn state: tx=%s not related to conn=%q (conn have current tx=%q)",
6161
tx.conn.currentTx.ID(), tx.conn.ID(), tx.ID(),
6262
)
6363
}

internal/xerrors/tx.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package xerrors
2+
3+
type alreadyHasTxError struct {
4+
currentTx string
5+
}
6+
7+
func (err *alreadyHasTxError) Error() string {
8+
return "сonn already has an open currentTx: " + err.currentTx
9+
}
10+
11+
func (err *alreadyHasTxError) As(target interface{}) bool {
12+
switch t := target.(type) {
13+
case *alreadyHasTxError:
14+
t.currentTx = err.currentTx
15+
16+
return true
17+
default:
18+
return false
19+
}
20+
}
21+
22+
func AlreadyHasTx(txID string) error {
23+
return &alreadyHasTxError{currentTx: txID}
24+
}
25+
26+
func IsAlreadyHasTx(err error) bool {
27+
var txErr *alreadyHasTxError
28+
return As(err, &txErr)
29+
}

tests/integration/tx_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/require"
1111

1212
"github.com/ydb-platform/ydb-go-sdk/v3"
13-
tableSql "github.com/ydb-platform/ydb-go-sdk/v3/internal/table/conn"
13+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
1414
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
1515
"github.com/ydb-platform/ydb-go-sdk/v3/table"
1616
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/indexed"
@@ -137,15 +137,12 @@ func TestNoEffectsIfForgetCommitTx(t *testing.T) {
137137
require.NoError(t, err)
138138

139139
// check row for NO write
140-
var (
141-
value string
142-
connAlreadyHaveTxError *tableSql.AlreadyHaveTxError
143-
)
140+
var value string
144141
err = db.QueryRowContext(ctx, `SELECT val FROM table WHERE id = $1`, id).Scan(&value)
145142
require.ErrorIs(t, err, sql.ErrNoRows)
146143

147144
// second tx on existing conn === session
148145
_, err = cc.BeginTx(ctx, &sql.TxOptions{})
149-
require.ErrorAs(t, err, &connAlreadyHaveTxError)
146+
require.True(t, xerrors.IsAlreadyHasTx(err))
150147
})
151148
}

0 commit comments

Comments
 (0)