Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors not having a message property #45

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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