Skip to content

Commit

Permalink
fix for openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
peze committed Oct 29, 2024
1 parent 49b58a0 commit bbc7b3b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
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

0 comments on commit bbc7b3b

Please sign in to comment.