From 036edd8b1b33d1e94ffd8aa8dad85f5a9adcb0c0 Mon Sep 17 00:00:00 2001 From: "gianluca.casagrande" Date: Thu, 27 Mar 2025 16:54:12 +0100 Subject: [PATCH 1/2] error message fix --- src/Deferred.ts | 2 +- test/integration/pool-test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Deferred.ts b/src/Deferred.ts index 378eaa30..76e1b10d 100644 --- a/src/Deferred.ts +++ b/src/Deferred.ts @@ -23,7 +23,7 @@ export class Deferred { this._timeout = setTimeout(() => { callback(); - this.reject(new TimeoutError('Operation timeout')); + this.reject(new TimeoutError('Operation timeout: pool acquire')); }, timeoutInMillis); } diff --git a/test/integration/pool-test.ts b/test/integration/pool-test.ts index 522bd8fd..906dcbaf 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 === 'Operation timeout: pool acquire'); pool.release(obj); t.end(); }); From a44d5dd9633e8b8754d9993b1c78bed520fde357 Mon Sep 17 00:00:00 2001 From: "gianluca.casagrande" Date: Fri, 28 Mar 2025 08:47:18 +0100 Subject: [PATCH 2/2] feat: add customizable error messages for Deferred timeout --- src/Deferred.ts | 11 +++++++++-- src/Pool.ts | 4 +++- test/integration/pool-test.ts | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Deferred.ts b/src/Deferred.ts index 76e1b10d..a4891ea5 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: pool acquire')); + this.reject( + new TimeoutError(this.options.errorMessage ?? 'Operation timeout'), + ); }, timeoutInMillis); } diff --git a/src/Pool.ts b/src/Pool.ts index 33036fc3..92b974e7 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 906dcbaf..566b4252 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: pool acquire'); + t.ok(e.message === 'Pool acquire operation timeout'); pool.release(obj); t.end(); });