Skip to content

Commit d5c65a4

Browse files
committed
handle session error on server response instead of callback error
1 parent dcd59fa commit d5c65a4

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

internal/query/client.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ func do(
212212

213213
err := op(ctx, s)
214214
if err != nil {
215-
s.SetStatus(session.StatusError)
216-
217215
return xerrors.WithStackTrace(err)
218216
}
219217

@@ -276,10 +274,6 @@ func doTx(
276274

277275
defer func() {
278276
_ = tx.Rollback(ctx)
279-
280-
if opErr != nil {
281-
s.SetStatus(session.StatusError)
282-
}
283277
}()
284278

285279
err = op(ctx, tx)

internal/query/session.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
7+
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
78

89
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
910
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
@@ -38,6 +39,8 @@ func (s *Session) QueryResultSet(
3839

3940
r, err := execute(ctx, s.ID(), s.client, q, options.ExecuteSettings(opts...), withTrace(s.trace))
4041
if err != nil {
42+
s.handleSessionErrorStatus(err)
43+
4144
return nil, xerrors.WithStackTrace(err)
4245
}
4346

@@ -54,6 +57,8 @@ func (s *Session) queryRow(
5457
) (row query.Row, finalErr error) {
5558
r, err := execute(ctx, s.ID(), s.client, q, settings, resultOpts...)
5659
if err != nil {
60+
s.handleSessionErrorStatus(err)
61+
5762
return nil, xerrors.WithStackTrace(err)
5863
}
5964

@@ -120,6 +125,8 @@ func (s *Session) Begin(
120125

121126
txID, err := begin(ctx, s.client, s.ID(), txSettings)
122127
if err != nil {
128+
s.handleSessionErrorStatus(err)
129+
123130
return nil, xerrors.WithStackTrace(err)
124131
}
125132

@@ -140,6 +147,8 @@ func (s *Session) Exec(
140147

141148
r, err := execute(ctx, s.ID(), s.client, q, options.ExecuteSettings(opts...), withTrace(s.trace))
142149
if err != nil {
150+
s.handleSessionErrorStatus(err)
151+
143152
return xerrors.WithStackTrace(err)
144153
}
145154

@@ -162,8 +171,19 @@ func (s *Session) Query(
162171

163172
r, err := execute(ctx, s.ID(), s.client, q, options.ExecuteSettings(opts...), withTrace(s.trace))
164173
if err != nil {
174+
s.handleSessionErrorStatus(err)
175+
165176
return nil, xerrors.WithStackTrace(err)
166177
}
167178

168179
return r, nil
169180
}
181+
182+
func (s *Session) handleSessionErrorStatus(err error) {
183+
switch {
184+
case xerrors.IsTransportError(err) || xerrors.IsOperationError(err, Ydb.StatusIds_SESSION_BUSY, Ydb.StatusIds_BAD_SESSION):
185+
s.SetStatus(session.StatusError)
186+
case xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION):
187+
s.SetStatus(session.StatusClosed)
188+
}
189+
}

internal/query/transaction.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"fmt"
66

77
"github.com/ydb-platform/ydb-go-genproto/Ydb_Query_V1"
8-
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
98
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
109

1110
"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
1211
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
1312
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/result"
14-
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/session"
1513
queryTx "github.com/ydb-platform/ydb-go-sdk/v3/internal/query/tx"
1614
"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
1715
baseTx "github.com/ydb-platform/ydb-go-sdk/v3/internal/tx"
@@ -338,10 +336,7 @@ func (tx *Transaction) CommitTx(ctx context.Context) (finalErr error) {
338336

339337
err = commitTx(ctx, tx.s.client, tx.s.ID(), tx.ID())
340338
if err != nil {
341-
if xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION) {
342-
tx.s.SetStatus(session.StatusClosed)
343-
}
344-
339+
tx.s.handleSessionErrorStatus(err)
345340
return xerrors.WithStackTrace(err)
346341
}
347342

@@ -376,9 +371,7 @@ func (tx *Transaction) Rollback(ctx context.Context) (finalErr error) {
376371

377372
err := rollback(ctx, tx.s.client, tx.s.ID(), tx.ID())
378373
if err != nil {
379-
if xerrors.IsOperationError(err, Ydb.StatusIds_BAD_SESSION) {
380-
tx.s.SetStatus(session.StatusClosed)
381-
}
374+
tx.s.handleSessionErrorStatus(err)
382375

383376
return xerrors.WithStackTrace(err)
384377
}

0 commit comments

Comments
 (0)