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 for openapi #57

Merged
merged 1 commit into from
Oct 29, 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
22 changes: 16 additions & 6 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@


import { Request, Response } from './core';
import { RetryPolicyContext } from './retry';

export class BaseError extends Error {
name: string;
Expand Down Expand Up @@ -70,10 +71,19 @@ export function newError(data: any): ResponseError {
return new ResponseError(data);
}

export function newUnretryableError(request: Request): Error {
const e = new UnretryableError('');
e.data = {
lastRequest: request
};
return e;
export function newUnretryableError(ctx: RetryPolicyContext | Request): Error {
if(ctx instanceof RetryPolicyContext) {
const message = ctx.exception ? ctx.exception.message : '';
const e = new UnretryableError(message);
e.data = {
lastRequest: ctx.httpRequest
};
return e;
} else {
const e = new UnretryableError('');
e.data = {
lastRequest: ctx
};
return e;
}
}
3 changes: 3 additions & 0 deletions src/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ export class RetryPolicyContext {
}

export function shouldRetry(options: RetryOptions, ctx: RetryPolicyContext): boolean {
if(ctx.retriesAttempted === 0) {
return true;
}
if(!options || !options.retryable) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion test/error.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('$dara error', function () {
});

it('newUnretryableError should ok', function () {
const err = $dara.newUnretryableError(new $dara.Request());
const err = $dara.newUnretryableError(new $dara.RetryPolicyContext({}));
assert.strictEqual(err.name, 'UnretryableError');
});

Expand Down
1 change: 0 additions & 1 deletion test/fixtures/test.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Test For File
6 changes: 6 additions & 0 deletions test/retry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ describe('$dara retry', function () {
assert.deepStrictEqual($dara.shouldRetry(undefined, context), false);
assert.deepStrictEqual($dara.shouldRetry(null, context), false);

context = new $dara.RetryPolicyContext({
retriesAttempted: 0,
});
assert.deepStrictEqual($dara.shouldRetry(undefined, context), true);

const condition1 = new $dara.RetryCondition({
maxAttempts: 3,
exception: ['AErr'],
Expand Down Expand Up @@ -139,6 +144,7 @@ describe('$dara retry', function () {
});
assert.deepStrictEqual($dara.shouldRetry(option, context), false);
context = new $dara.RetryPolicyContext({
retriesAttempted: 1,
exception: new BErr({
code: 'A1Err',
message: 'b1 error',
Expand Down