diff --git a/README.md b/README.md index c5072a4..3226a39 100644 --- a/README.md +++ b/README.md @@ -121,8 +121,8 @@ Wrapped function can throw `NotRetryableError` if retrying need to be stopped ev ```typescript import { NotRetryableError } from 'ts-retry-promise'; -retry(async () => throw new NotRetryableError("This error")) - .catch(err => console.log(err.lastError), { retries: 'INFINITELY' }); +retry(async () => { throw new NotRetryableError("This error") }, { retries: 'INFINITELY' }) + .catch(err => console.log(err.lastError.message)); // will print "This error" ``` ## Samples ## diff --git a/src/retry-promise.ts b/src/retry-promise.ts index 60134e4..ac0b92c 100644 --- a/src/retry-promise.ts +++ b/src/retry-promise.ts @@ -141,7 +141,7 @@ export class RetryError extends Error { // tslint:disable-next-line:max-classes-per-file class BaseError { - constructor (...args: unknown[]) { + constructor (public message?: string, ...args: unknown[]) { Error.apply(this, args as any); } } diff --git a/test/notretryable.error.test.ts b/test/notretryable.error.test.ts index cfaffdd..d48760c 100644 --- a/test/notretryable.error.test.ts +++ b/test/notretryable.error.test.ts @@ -11,6 +11,20 @@ describe("NotRetryableError Error", () => { expect(failer.calls).to.eq(1); }); + it("should have RetryError in lastError", async () => { + const error = new NotRetryableError("stop retrying"); + + const result = retry(async () => {throw error}); + + expect(result).to.be.eventually.rejected.with.property("lastError").eq(error); + }); + + it("RetryError should have message", async () => { + const error = new NotRetryableError("stop retrying"); + + expect(error.message).to.eq("stop retrying"); + }); + }); class Failer { diff --git a/test/retry-promise.demo.test.ts b/test/retry-promise.demo.test.ts index b993a16..77e27e0 100644 --- a/test/retry-promise.demo.test.ts +++ b/test/retry-promise.demo.test.ts @@ -1,5 +1,5 @@ import {expect} from "./index"; -import {customizeRetry, defaultRetryConfig, retry, retryDecorator, wait} from "../src/retry-promise"; +import {customizeRetry, defaultRetryConfig, NotRetryableError, retry, retryDecorator, wait} from "../src/retry-promise"; describe("Retry Promise Demo", () => { @@ -97,6 +97,17 @@ describe("Retry Promise Demo", () => { expect(profile.name).to.eq("Mr 123"); }) + it("NotRetryableError demo", async () => { + + const result = retry( + async () => { throw new NotRetryableError("This error"); }, + { retries: 'INFINITELY' }) + // tslint:disable-next-line:no-console + .catch(err => console.log(err.lastError.message)); // will print "This error" + + await result; + }) + }); const browser = {