Skip to content

Commit

Permalink
Fix errors not having a message property (#45)
Browse files Browse the repository at this point in the history
* Fix errors not having a message property
  • Loading branch information
normartin authored Jan 7, 2024
1 parent 67c0f45 commit 3599105
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ##
Expand Down
2 changes: 1 addition & 1 deletion src/retry-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/notretryable.error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion test/retry-promise.demo.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 3599105

Please sign in to comment.