diff --git a/readme.md b/readme.md index 5dd3ddd..760586c 100644 --- a/readme.md +++ b/readme.md @@ -472,7 +472,7 @@ export interface IRetryBackoffContext { * The result of the last method call. Either a thrown error, or a value * that we determined should be retried upon. */ - result: { error: Error } | { value: ReturnType }; + result: { error: unknown } | { value: ReturnType }; } ``` diff --git a/src/BulkheadPolicy.ts b/src/BulkheadPolicy.ts index d6ef786..04e6afe 100644 --- a/src/BulkheadPolicy.ts +++ b/src/BulkheadPolicy.ts @@ -10,7 +10,7 @@ interface IQueueItem { signal: AbortSignal; fn(context: IDefaultPolicyContext): Promise | T; resolve(value: T): void; - reject(error: Error): void; + reject(error: unknown): void; } export class BulkheadPolicy implements IPolicy { diff --git a/src/Policy.test.ts b/src/Policy.test.ts index 3af095b..df19174 100644 --- a/src/Policy.test.ts +++ b/src/Policy.test.ts @@ -86,7 +86,7 @@ describe('Policy', () => { handleType(MyError1) .orType(MyError2) .orType(MyError3, e => e.message === 'foo') - .orWhen(e => e.message === 'potato'), + .orWhen(e => e instanceof Error && e.message === 'potato'), { maxAttempts: 10 }, ).execute(fn), ).to.be.rejectedWith(MyError3, 'bar'); diff --git a/src/Policy.ts b/src/Policy.ts index 6404d59..2693554 100644 --- a/src/Policy.ts +++ b/src/Policy.ts @@ -18,7 +18,7 @@ const always = () => true; const never = () => false; export interface IBasePolicyOptions { - errorFilter: (error: Error) => boolean; + errorFilter: (error: unknown) => boolean; resultFilter: (result: unknown) => boolean; } @@ -26,7 +26,7 @@ export interface IBasePolicyOptions { * The reason for a call failure. Either an error, or the a value that was * marked as a failure (when using result filtering). */ -export type FailureReason = { error: Error } | { value: ReturnType }; +export type FailureReason = { error: unknown } | { value: ReturnType }; /** * Event emitted on the `onFailure` calls. @@ -165,7 +165,7 @@ export class Policy { * .execute(() => getJsonFrom('https://example.com')); * ``` */ - public orWhen(predicate: (error: Error) => boolean) { + public orWhen(predicate: (error: unknown) => boolean) { return new Policy({ ...this.options, errorFilter: e => this.options.errorFilter(e) || predicate(e), @@ -237,7 +237,7 @@ export function handleType(cls: Constructor, predicate?: (error: T) => boo /** * See {@link Policy.orWhen} for usage. */ -export function handleWhen(predicate: (error: Error) => boolean) { +export function handleWhen(predicate: (error: unknown) => boolean) { return new Policy({ errorFilter: predicate, resultFilter: never }); } /** diff --git a/src/common/Executor.ts b/src/common/Executor.ts index b395cc7..0a7d3af 100644 --- a/src/common/Executor.ts +++ b/src/common/Executor.ts @@ -34,7 +34,7 @@ export class ExecuteWrapper { public readonly onFailure = this.failureEmitter.addListener; constructor( - private readonly errorFilter: (error: Error) => boolean = () => false, + private readonly errorFilter: (error: unknown) => boolean = () => false, private readonly resultFilter: (result: unknown) => boolean = () => false, ) {} @@ -62,9 +62,8 @@ export class ExecuteWrapper { } return { value }; - } catch (rawError) { - const error = rawError as Error; - const handled = this.errorFilter(error as Error); + } catch (error) { + const handled = this.errorFilter(error); if (stopwatch) { this.failureEmitter.emit({ duration: stopwatch(), handled, reason: { error } }); } diff --git a/src/common/defer.ts b/src/common/defer.ts index d28fb29..3066f2a 100644 --- a/src/common/defer.ts +++ b/src/common/defer.ts @@ -1,6 +1,6 @@ export const defer = () => { let resolve: (value: T) => void; - let reject: (error: Error) => void; + let reject: (error: unknown) => void; const promise = new Promise((res, rej) => { resolve = res; reject = rej;