diff --git a/src/session-pool.ts b/src/session-pool.ts index a6ba7b601..1d55da60f 100644 --- a/src/session-pool.ts +++ b/src/session-pool.ts @@ -184,12 +184,26 @@ export class SessionLeakError extends Error { } } +/** + * Error to be thrown when the session pool is exhausted. + */ +export class SessionPoolExhaustedError extends Error { + messages: string[]; + constructor(leaks: string[]) { + super(errors.Exhausted); + // Restore error name that was overwritten by the super constructor call. + this.name = SessionPoolExhaustedError.name; + this.messages = leaks; + } +} + /** * enum to capture errors that can appear from multiple places */ const enum errors { Closed = 'Database is closed.', Timeout = 'Timeout occurred while acquiring session.', + Exhausted = 'No resources available.', } interface SessionInventory { @@ -723,8 +737,8 @@ export class SessionPool extends EventEmitter implements SessionPoolInterface { return this._borrowNextAvailableSession(type); } - if (this.options.fail!) { - throw new Error('No resources available.'); + if (this.isFull && this.options.fail!) { + throw new SessionPoolExhaustedError(this._getLeaks()); } let removeListener: Function;