diff --git a/src/PendingOperation.ts b/src/PendingOperation.ts index fa023d7..fab431c 100644 --- a/src/PendingOperation.ts +++ b/src/PendingOperation.ts @@ -1,4 +1,4 @@ -import { TimeoutError } from './TimeoutError'; +import { PendingOperationError, TimeoutError } from './errors'; import { defer, Deferred } from './utils'; export class PendingOperation { @@ -27,7 +27,7 @@ export class PendingOperation { } abort() { - this.reject(new Error('aborted')); + this.reject(new PendingOperationError('aborted')); } reject(err: Error) { diff --git a/src/Pool.ts b/src/Pool.ts index 9bcc90c..a221d64 100644 --- a/src/Pool.ts +++ b/src/Pool.ts @@ -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'; @@ -49,57 +50,57 @@ export class Pool { 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) ); @@ -123,7 +124,7 @@ export class Pool { 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}`); } } @@ -398,7 +399,7 @@ export class Pool { 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 diff --git a/src/TimeoutError.ts b/src/TimeoutError.ts deleted file mode 100644 index 18f20e4..0000000 --- a/src/TimeoutError.ts +++ /dev/null @@ -1 +0,0 @@ -export class TimeoutError extends Error {} diff --git a/src/errors.ts b/src/errors.ts new file mode 100644 index 0000000..d46c121 --- /dev/null +++ b/src/errors.ts @@ -0,0 +1,7 @@ +export class TarnError extends Error {} + +export class TimeoutError extends TarnError {} + +export class PendingOperationError extends TarnError {} + +export class PoolError extends TarnError {} diff --git a/src/tarn.ts b/src/tarn.ts index 0aa5053..60a5688 100644 --- a/src/tarn.ts +++ b/src/tarn.ts @@ -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 };