Skip to content

Commit 2cc44cf

Browse files
authored
fix: error from fill method should not be emitted (#2233)
Fixes #2103
1 parent 0008038 commit 2cc44cf

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/session-pool.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,7 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
783783
return;
784784
}
785785

786-
try {
787-
await this._createSessions(needed);
788-
} catch (e) {
789-
this.emit('error', e);
790-
}
786+
await this._createSessions(needed);
791787
}
792788

793789
/**
@@ -993,7 +989,24 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
993989
const pings = sessions.map(session => this._ping(session));
994990

995991
await Promise.all(pings);
996-
return this._fill();
992+
try {
993+
await this._fill();
994+
} catch (error) {
995+
// Ignore `Database not found` error. This allows a user to call instance.database('db-name')
996+
// for a database that does not yet exist with SessionPoolOptions.min > 0.
997+
const err = error as ServiceError;
998+
if (
999+
isDatabaseNotFoundError(err) ||
1000+
isInstanceNotFoundError(err) ||
1001+
isCreateSessionPermissionError(err) ||
1002+
isDefaultCredentialsNotSetError(err) ||
1003+
isProjectIdNotSetInEnvironmentError(err)
1004+
) {
1005+
return;
1006+
}
1007+
this.emit('error', err);
1008+
}
1009+
return;
9971010
}
9981011

9991012
/**

test/session-pool.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -953,12 +953,10 @@ describe('SessionPool', () => {
953953

954954
stub.rejects(error);
955955

956-
sessionPool.once('error', err => {
956+
sessionPool._fill().catch(err => {
957957
assert.strictEqual(err, error);
958958
done();
959959
});
960-
961-
sessionPool._fill();
962960
});
963961
});
964962

@@ -1286,6 +1284,23 @@ describe('SessionPool', () => {
12861284

12871285
assert.strictEqual(fillStub.callCount, 1);
12881286
});
1287+
1288+
it('should not throw error when database not found', async () => {
1289+
const fakeSessions = [createSession()];
1290+
sandbox.stub(sessionPool, '_getIdleSessions').returns(fakeSessions);
1291+
1292+
const error = {
1293+
code: grpc.status.NOT_FOUND,
1294+
message: 'Database not found',
1295+
} as grpc.ServiceError;
1296+
sandbox.stub(sessionPool, '_fill').rejects(error);
1297+
1298+
try {
1299+
await sessionPool._pingIdleSessions();
1300+
} catch (err) {
1301+
assert.ifError(err);
1302+
}
1303+
});
12891304
});
12901305

12911306
describe('_release', () => {

0 commit comments

Comments
 (0)