diff --git a/src/Deferred.ts b/src/Deferred.ts index 378eaa3..a4891ea 100644 --- a/src/Deferred.ts +++ b/src/Deferred.ts @@ -1,5 +1,8 @@ import { TimeoutError } from './TimeoutError'; +interface DeferredOptions { + errorMessage?: string; +} /** * Deferred Implementation * @@ -10,8 +13,10 @@ export class Deferred { protected _resolve: (value: T) => void; protected _reject: (error: Error) => void; protected _timeout: NodeJS.Timer; + private options: DeferredOptions; - constructor() { + constructor(options: DeferredOptions = {}) { + this.options = options; this._promise = new Promise((resolve, reject) => { this._reject = reject; this._resolve = resolve; @@ -23,7 +28,9 @@ export class Deferred { this._timeout = setTimeout(() => { callback(); - this.reject(new TimeoutError('Operation timeout')); + this.reject( + new TimeoutError(this.options.errorMessage ?? 'Operation timeout'), + ); }, timeoutInMillis); } diff --git a/src/Pool.ts b/src/Pool.ts index 33036fc..92b974e 100644 --- a/src/Pool.ts +++ b/src/Pool.ts @@ -433,7 +433,9 @@ export class Pool { ); } - const deferred = new Deferred(); + const deferred = new Deferred({ + errorMessage: 'Pool acquire operation timeout', + }); deferred.registerTimeout(this.acquireTimeoutMillis, () => { // timeout triggered, promise will be rejected // remove this object from pending list diff --git a/test/integration/pool-test.ts b/test/integration/pool-test.ts index 522bd8f..566b425 100644 --- a/test/integration/pool-test.ts +++ b/test/integration/pool-test.ts @@ -23,7 +23,7 @@ tap.test('pool expands only to max limit', (t) => { .then((obj) => { return pool.acquire().catch((e) => { t.ok(e instanceof TimeoutError); - t.ok(e.message === 'Operation timeout'); + t.ok(e.message === 'Pool acquire operation timeout'); pool.release(obj); t.end(); });