Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/PendingOperation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TimeoutError } from './TimeoutError';
import { PendingOperationError, TimeoutError } from './errors';
import { defer, Deferred } from './utils';

export class PendingOperation<T> {
Expand Down Expand Up @@ -27,7 +27,7 @@ export class PendingOperation<T> {
}

abort() {
this.reject(new Error('aborted'));
this.reject(new PendingOperationError('aborted'));
}

reject(err: Error) {
Expand Down
27 changes: 14 additions & 13 deletions src/Pool.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PoolError } from './errors';
import { PendingOperation } from './PendingOperation';
import { Resource } from './Resource';
import { checkOptionalTime, delay, duration, now, reflect, tryPromise } from './utils';
Expand Down Expand Up @@ -49,57 +50,57 @@ export class Pool<T> {
opt = opt || {};

if (!opt.create) {
throw new Error('Tarn: opt.create function most be provided');
throw new PoolError('Tarn: opt.create function most be provided');
}

if (!opt.destroy) {
throw new Error('Tarn: opt.destroy function most be provided');
throw new PoolError('Tarn: opt.destroy function most be provided');
}

if (typeof opt.min !== 'number' || opt.min < 0 || opt.min !== Math.round(opt.min)) {
throw new Error('Tarn: opt.min must be an integer >= 0');
throw new PoolError('Tarn: opt.min must be an integer >= 0');
}

if (typeof opt.max !== 'number' || opt.max <= 0 || opt.max !== Math.round(opt.max)) {
throw new Error('Tarn: opt.max must be an integer > 0');
throw new PoolError('Tarn: opt.max must be an integer > 0');
}

if (opt.min > opt.max) {
throw new Error('Tarn: opt.max is smaller than opt.min');
throw new PoolError('Tarn: opt.max is smaller than opt.min');
}

if (!checkOptionalTime(opt.acquireTimeoutMillis)) {
throw new Error(
throw new PoolError(
'Tarn: invalid opt.acquireTimeoutMillis ' + JSON.stringify(opt.acquireTimeoutMillis)
);
}

if (!checkOptionalTime(opt.createTimeoutMillis)) {
throw new Error(
throw new PoolError(
'Tarn: invalid opt.createTimeoutMillis ' + JSON.stringify(opt.createTimeoutMillis)
);
}

if (!checkOptionalTime(opt.destroyTimeoutMillis)) {
throw new Error(
throw new PoolError(
'Tarn: invalid opt.destroyTimeoutMillis ' + JSON.stringify(opt.destroyTimeoutMillis)
);
}

if (!checkOptionalTime(opt.idleTimeoutMillis)) {
throw new Error(
throw new PoolError(
'Tarn: invalid opt.idleTimeoutMillis ' + JSON.stringify(opt.idleTimeoutMillis)
);
}

if (!checkOptionalTime(opt.reapIntervalMillis)) {
throw new Error(
throw new PoolError(
'Tarn: invalid opt.reapIntervalMillis ' + JSON.stringify(opt.reapIntervalMillis)
);
}

if (!checkOptionalTime(opt.createRetryIntervalMillis)) {
throw new Error(
throw new PoolError(
'Tarn: invalid opt.createRetryIntervalMillis ' +
JSON.stringify(opt.createRetryIntervalMillis)
);
Expand All @@ -123,7 +124,7 @@ export class Pool<T> {

for (const key of Object.keys(opt)) {
if (!allowedKeys[key]) {
throw new Error(`Tarn: unsupported option opt.${key}`);
throw new PoolError(`Tarn: unsupported option opt.${key}`);
}
}

Expand Down Expand Up @@ -398,7 +399,7 @@ export class Pool<T> {
if (free === undefined || pendingAcquire === undefined) {
const errMessage = 'this.free was empty while trying to acquire resource';
this.log(`Tarn: ${errMessage}`, 'warn');
throw new Error(`Internal error, should never happen. ${errMessage}`);
throw new PoolError(`Internal error, should never happen. ${errMessage}`);
}

// Make sure that pendingAcquire that is being validated is not lost and
Expand Down
1 change: 0 additions & 1 deletion src/TimeoutError.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class TarnError extends Error {}

export class TimeoutError extends TarnError {}

export class PendingOperationError extends TarnError {}

export class PoolError extends TarnError {}
7 changes: 4 additions & 3 deletions src/tarn.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Pool } from './Pool';
import { TimeoutError } from './TimeoutError';
import * as errors from './errors';

export { Pool, TimeoutError };
export { Pool };
export * from './errors';

module.exports = {
Pool,
TimeoutError
...errors
};