Skip to content

Commit

Permalink
add original message to the error thrown if available (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcfvct authored Apr 25, 2021
1 parent 93036c8 commit 58ca3cc
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [12.x, 14.x]

steps:
- uses: actions/checkout@v2
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
registry-url: https://registry.npmjs.org/
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
15 changes: 8 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module.exports = {
moduleFileExtensions: ["ts", "js", "json"],
moduleFileExtensions: ['ts', 'js', 'json'],
transform: {
"^.+\\.(ts)$": "ts-jest"
'^.+\\.(ts)$': 'ts-jest',
},
globals: {
"ts-jest": {
tsconfig: "tsconfig.json"
}
'ts-jest': {
tsconfig: 'tsconfig.json',
},
},
testMatch: ["**/*.test.ts"],
testPathIgnorePatterns: ["/node_modules/", "/dist/", "/lib/"],
testMatch: ['**/*.test.ts'],
testPathIgnorePatterns: ['/node_modules/', '/dist/', '/lib/'],
collectCoverage: true,
'coverageReporters': ['json', 'html'],
verbose: true,
};
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": "1.5.1",
"version": "2.0.0",
"description": "A simple retry decorator for typescript with no dependency.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
12 changes: 8 additions & 4 deletions src/retry.decorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,22 @@ describe('Retry Test', () => {

test('normal retry', async () => {
const calledSpy = jest.spyOn(testClass, 'called');
calledSpy.mockRejectedValueOnce('bad');
calledSpy.mockResolvedValueOnce('good');
calledSpy.mockRejectedValueOnce(new Error('rejected'));
calledSpy.mockResolvedValueOnce('fulfilled');
await testClass.testMethod();
expect(calledSpy).toHaveBeenCalledTimes(2);
});

test('exceed max retry', async () => {
const calledSpy = jest.spyOn(testClass, 'called');
calledSpy.mockRejectedValue('bad');
const errorMsg = 'rejected';
calledSpy.mockRejectedValue(new Error(errorMsg));
try {
await testClass.testMethod();
} catch (e) {}
} catch (e) {
expect(e).not.toBeUndefined();
expect(e.message.includes(errorMsg));
}
expect(calledSpy).toHaveBeenCalledTimes(3);
});

Expand Down
17 changes: 13 additions & 4 deletions src/retry.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export function Retryable(options: RetryOptions): Function {
try {
return await retryAsync.apply(this, [originalFn, args, options.maxAttempts, options.backOff]);
} catch (e) {
if (e.message === 'maxAttempts') {
e.code = '429';
e.message = `Failed for '${propertyKey}' for ${options.maxAttempts} times.`;
if (e instanceof MaxAttemptsError) {
const msgPrefix = `Failed for '${propertyKey}' for ${options.maxAttempts} times.`;
e.message = e.message ? `${msgPrefix} Original Error: ${e.message}` : msgPrefix;
}
throw e;
}
Expand All @@ -41,7 +41,7 @@ export function Retryable(options: RetryOptions): Function {
} catch (e) {
if (--maxAttempts < 0) {
e?.message && console.error(e.message);
throw new Error('maxAttempts');
throw new MaxAttemptsError(e?.message);
}
if (!canRetry(e)) {
throw e;
Expand Down Expand Up @@ -74,6 +74,15 @@ export function Retryable(options: RetryOptions): Function {
}
}

export class MaxAttemptsError extends Error {
code = '429'
/* if target is ES5, need the 'new.target.prototype'
constructor(msg?: string) {
super(msg)
Object.setPrototypeOf(this, new.target.prototype)
} */
}

export interface RetryOptions {
maxAttempts: number;
backOffPolicy?: BackOffPolicy;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES5",
"target": "ES2015",
"noImplicitAny": true,
"declaration": true,
"moduleResolution": "node",
Expand Down

0 comments on commit 58ca3cc

Please sign in to comment.