Skip to content

Commit

Permalink
fix: error from fill method should not be emitted (#2233)
Browse files Browse the repository at this point in the history
Fixes #2103
  • Loading branch information
surbhigarg92 authored Feb 17, 2025
1 parent 0008038 commit 2cc44cf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
25 changes: 19 additions & 6 deletions src/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,11 +783,7 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface {
return;
}

try {
await this._createSessions(needed);
} catch (e) {
this.emit('error', e);
}
await this._createSessions(needed);
}

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

await Promise.all(pings);
return this._fill();
try {
await this._fill();
} catch (error) {
// Ignore `Database not found` error. This allows a user to call instance.database('db-name')
// for a database that does not yet exist with SessionPoolOptions.min > 0.
const err = error as ServiceError;
if (
isDatabaseNotFoundError(err) ||
isInstanceNotFoundError(err) ||
isCreateSessionPermissionError(err) ||
isDefaultCredentialsNotSetError(err) ||
isProjectIdNotSetInEnvironmentError(err)
) {
return;
}
this.emit('error', err);
}
return;
}

/**
Expand Down
21 changes: 18 additions & 3 deletions test/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,12 +953,10 @@ describe('SessionPool', () => {

stub.rejects(error);

sessionPool.once('error', err => {
sessionPool._fill().catch(err => {
assert.strictEqual(err, error);
done();
});

sessionPool._fill();
});
});

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

assert.strictEqual(fillStub.callCount, 1);
});

it('should not throw error when database not found', async () => {
const fakeSessions = [createSession()];
sandbox.stub(sessionPool, '_getIdleSessions').returns(fakeSessions);

const error = {
code: grpc.status.NOT_FOUND,
message: 'Database not found',
} as grpc.ServiceError;
sandbox.stub(sessionPool, '_fill').rejects(error);

try {
await sessionPool._pingIdleSessions();
} catch (err) {
assert.ifError(err);
}
});
});

describe('_release', () => {
Expand Down

0 comments on commit 2cc44cf

Please sign in to comment.