Skip to content

Commit

Permalink
Add option to not log through console (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleborrego authored Dec 21, 2022
1 parent a407db2 commit 371226f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Import and use it. Retry for `Promise` is supported as long as the `runtime` has
| exponentialOption | object | No | { maxInterval: 2000, multiplier: 2 } | This is for the `ExponentialBackOffPolicy` <br/> The max interval each wait and the multiplier for the `backOff`. |
| doRetry | (e: any) => boolean | No | - | Function with error parameter to decide if repetition is necessary. |
| value | Error/Exception class | No | [ ] | An array of Exception types that are retryable. |
| useConsoleLogger | boolean | No | true | Print errors on console. |

### Example
```typescript
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/retry.decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class TestClass {
await this.called();
}

@Retryable({ maxAttempts: 2, useConsoleLogger: false })
async noLog(): Promise<void> {
console.log(`test method is called for ${++this.count} time`);
await this.called();
}

async called(): Promise<string> {
return 'from real implementation';
}
Expand Down Expand Up @@ -153,5 +159,15 @@ describe('Retry Test', () => {
} catch (e) {}
expect(calledSpy).toHaveBeenCalledTimes(4);
});

test('no log', async () => {
const calledSpy = jest.spyOn(testClass, 'called');
const errorSpy = jest.spyOn(console, 'error');
calledSpy.mockRejectedValueOnce(new Error('rejected'));
calledSpy.mockResolvedValueOnce('fulfilled');
await testClass.testMethod();
expect(calledSpy).toHaveBeenCalledTimes(2);
expect(errorSpy).not.toHaveBeenCalled();
});
});

3 changes: 2 additions & 1 deletion src/retry.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function Retryable(options: RetryOptions): DecoratorFunction {
return await fn.apply(this, args);
} catch (e) {
if (--maxAttempts < 0) {
e?.message && console.error(e.message);
(typeof options.useConsoleLogger !== 'boolean' || options.useConsoleLogger) && e?.message && console.error(e.message);
const maxAttemptsErrorInstance = new MaxAttemptsError(e?.message);
// Add the existing error stack if present
if(e?.stack) {
Expand Down Expand Up @@ -96,6 +96,7 @@ export interface RetryOptions {
doRetry?: (e: any) => boolean;
value?: ErrorConstructor[];
exponentialOption?: { maxInterval: number; multiplier: number };
useConsoleLogger?: boolean;
}

export enum BackOffPolicy {
Expand Down

0 comments on commit 371226f

Please sign in to comment.