Skip to content

Commit cf2b74d

Browse files
change: descriptive pool acquire timeout error message (#57)
1 parent da269d7 commit cf2b74d

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/Deferred.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { TimeoutError } from './TimeoutError';
22

3+
interface DeferredOptions {
4+
errorMessage?: string;
5+
}
36
/**
47
* Deferred Implementation
58
*
@@ -10,8 +13,10 @@ export class Deferred<T> {
1013
protected _resolve: (value: T) => void;
1114
protected _reject: (error: Error) => void;
1215
protected _timeout: NodeJS.Timer;
16+
private options: DeferredOptions;
1317

14-
constructor() {
18+
constructor(options: DeferredOptions = {}) {
19+
this.options = options;
1520
this._promise = new Promise((resolve, reject) => {
1621
this._reject = reject;
1722
this._resolve = resolve;
@@ -23,7 +28,9 @@ export class Deferred<T> {
2328

2429
this._timeout = setTimeout(() => {
2530
callback();
26-
this.reject(new TimeoutError('Operation timeout'));
31+
this.reject(
32+
new TimeoutError(this.options.errorMessage ?? 'Operation timeout'),
33+
);
2734
}, timeoutInMillis);
2835
}
2936

src/Pool.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ export class Pool<RawResource> {
433433
);
434434
}
435435

436-
const deferred = new Deferred<RawResource>();
436+
const deferred = new Deferred<RawResource>({
437+
errorMessage: 'Pool acquire operation timeout',
438+
});
437439
deferred.registerTimeout(this.acquireTimeoutMillis, () => {
438440
// timeout triggered, promise will be rejected
439441
// remove this object from pending list

test/integration/pool-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tap.test('pool expands only to max limit', (t) => {
2323
.then((obj) => {
2424
return pool.acquire().catch((e) => {
2525
t.ok(e instanceof TimeoutError);
26-
t.ok(e.message === 'Operation timeout');
26+
t.ok(e.message === 'Pool acquire operation timeout');
2727
pool.release(obj);
2828
t.end();
2929
});

0 commit comments

Comments
 (0)