Skip to content

Commit

Permalink
fix: unhandled exception error catch (#2091)
Browse files Browse the repository at this point in the history
* fix: unhandled exception error catch

* refactor: test

* lint fix

* test: catch error

* test: for aborted and unavailable error

* test: refactor

* lint fix

* refactor

* fix for begin transaction called twice

---------

Co-authored-by: surbhigarg92 <[email protected]>
  • Loading branch information
alkatrivedi and surbhigarg92 authored Aug 9, 2024
1 parent 7d3b1c2 commit e277752
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ system-test/*key.json
.DS_Store
package-lock.json
__pycache__
.vscode
25 changes: 21 additions & 4 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,16 @@ export class Snapshot extends EventEmitter {
this._update(response.metadata!.transaction);
}
})
.on('error', () => {
if (!this.id && this._useInRunner) {
.on('error', err => {
const isServiceError = err && typeof err === 'object' && 'code' in err;
if (
!this.id &&
this._useInRunner &&
!(
isServiceError &&
(err as grpc.ServiceError).code === grpc.status.ABORTED
)
) {
this.begin();
}
});
Expand Down Expand Up @@ -1219,8 +1227,16 @@ export class Snapshot extends EventEmitter {
this._update(response.metadata!.transaction);
}
})
.on('error', () => {
if (!this.id && this._useInRunner) {
.on('error', err => {
const isServiceError = err && typeof err === 'object' && 'code' in err;
if (
!this.id &&
this._useInRunner &&
!(
isServiceError &&
(err as grpc.ServiceError).code === grpc.status.ABORTED
)
) {
this.begin();
}
});
Expand Down Expand Up @@ -1437,6 +1453,7 @@ export class Snapshot extends EventEmitter {
this._waitingRequests.push(() => {
makeRequest(resumeToken)
.on('data', chunk => streamProxy.emit('data', chunk))
.on('error', err => streamProxy.emit('error', err))
.on('end', () => streamProxy.emit('end'));
});

Expand Down
4 changes: 4 additions & 0 deletions test/mockserver/mockspanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ export class MockSpanner {
call.request!.transaction.id
}`;
if (this.abortedTransactions.has(fullTransactionId)) {
call.sendMetadata(new Metadata());
call.emit(
'error',
MockSpanner.createTransactionAbortedError(`${fullTransactionId}`)
Expand All @@ -556,6 +557,7 @@ export class MockSpanner {
call.request!.transaction.begin
);
if (txn instanceof Error) {
call.sendMetadata(new Metadata());
call.emit('error', txn);
call.end();
return;
Expand Down Expand Up @@ -593,6 +595,7 @@ export class MockSpanner {
index
);
if (streamErr) {
call.sendMetadata(new Metadata());
call.emit('error', streamErr);
break;
}
Expand All @@ -610,6 +613,7 @@ export class MockSpanner {
1
);
if (streamErr) {
call.sendMetadata(new Metadata());
call.emit('error', streamErr);
break;
}
Expand Down
Loading

0 comments on commit e277752

Please sign in to comment.