Skip to content

Commit

Permalink
Custom error constructor (#30)
Browse files Browse the repository at this point in the history
* use custom error constructor

* bump version
  • Loading branch information
vcfvct authored Jul 6, 2023
1 parent 152dfd0 commit 2952870
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-retry-decorator",
"version": "2.3.0",
"version": "2.4.0",
"description": "A simple retry decorator for typescript with no dependency.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
31 changes: 21 additions & 10 deletions src/retry.decorator.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import {BackOffPolicy, ExponentialBackoffStrategy, MaxAttemptsError, Retryable} from './retry.decorator';
import { BackOffPolicy, ExponentialBackoffStrategy, MaxAttemptsError, Retryable } from './retry.decorator';


// CustomError for testing.
class CustomError extends Error {
constructor(message?: string) {
// Call the parent class constructor with the provided message
super(message);

// Set the prototype and name properties
Object.setPrototypeOf(this, CustomError.prototype);
this.name = 'CustomError';
}
}

class TestClass {
count: number;
Expand All @@ -11,7 +24,7 @@ class TestClass {
await this.called();
}

@Retryable({ maxAttempts: 2, value: [SyntaxError, ReferenceError] })
@Retryable({ maxAttempts: 3, value: [SyntaxError, ReferenceError, CustomError] })
async testMethodWithException(): Promise<void> {
console.log(`test method is called for ${++this.count} time`);
await this.called();
Expand Down Expand Up @@ -125,7 +138,7 @@ describe('Retry Test', () => {

test('retry with specific error', async () => {
const calledSpy = jest.spyOn(testClass, 'called');
calledSpy.mockImplementationOnce(() => { throw new SyntaxError('I failed!'); });
calledSpy.mockImplementationOnce(() => { throw new CustomError('I failed!'); });
await testClass.testMethodWithException();
expect(calledSpy).toHaveBeenCalledTimes(2);
});
Expand All @@ -135,7 +148,7 @@ describe('Retry Test', () => {
calledSpy.mockImplementationOnce(() => { throw new Error('I failed!'); });
try {
await testClass.testMethodWithException();
} catch (e) {}
} catch (e) { }
expect(calledSpy).toHaveBeenCalledTimes(1);
});

Expand All @@ -152,7 +165,7 @@ describe('Retry Test', () => {
calledSpy.mockImplementationOnce(() => { throw new Error('Error: 500'); });
try {
await testClass.testDoRetry();
} catch (e) {}
} catch (e) { }
expect(calledSpy).toHaveBeenCalledTimes(1);
});

Expand All @@ -161,7 +174,7 @@ describe('Retry Test', () => {
calledSpy.mockImplementation(() => { throw new Error('Error: 500'); });
try {
await testClass.fixedBackOffRetry();
} catch (e) {}
} catch (e) { }
expect(calledSpy).toHaveBeenCalledTimes(4);
});

Expand All @@ -171,7 +184,7 @@ describe('Retry Test', () => {
calledSpy.mockImplementation(() => { throw new Error(); });
try {
await testClass.exponentialBackOffRetry();
} catch (e) {}
} catch (e) { }
expect(calledSpy).toHaveBeenCalledTimes(4);
});

Expand All @@ -181,7 +194,7 @@ describe('Retry Test', () => {
calledSpy.mockImplementation(() => { throw new Error(); });
try {
await testClass.exponentialBackOffWithJitterRetry();
} catch (e) {}
} catch (e) { }
expect(calledSpy).toHaveBeenCalledTimes(4);
});

Expand All @@ -195,8 +208,6 @@ describe('Retry Test', () => {
expect(errorSpy).not.toHaveBeenCalled();
});

// CustomError for testing.
class CustomError extends Error { }

test('throw original error', async () => {
jest.setTimeout(60000);
Expand Down
6 changes: 5 additions & 1 deletion src/retry.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export class MaxAttemptsError extends Error {
} */
}

interface ConstructableError {
new (...args: any[]): Error;
}

export interface RetryOptions {
backOffPolicy?: BackOffPolicy;
backOff?: number;
Expand All @@ -119,7 +123,7 @@ export interface RetryOptions {
backoffStrategy?: ExponentialBackoffStrategy;
};
maxAttempts: number;
value?: ErrorConstructor[];
value?: ConstructableError[];
useConsoleLogger?: boolean;
useOriginalError?: boolean;
}
Expand Down

0 comments on commit 2952870

Please sign in to comment.