Skip to content
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
1 change: 1 addition & 0 deletions packages/client/src/core/should-retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ export function shouldRetry({ attempt, method, retry, error }: ShouldRetryParams
return false;
}

// Non-transient errors (e.g. validation failures) are not retried.
return false;
}
28 changes: 28 additions & 0 deletions packages/client/tests/integration/response-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,32 @@ describe('response validation', () => {

await expect(client.get('/users/1')).resolves.toEqual({ id: 'user-1' });
});

it('does not retry when response validation fails', async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ name: 'Roman' }), {
status: 200,
headers: {
'content-type': 'application/json',
},
}),
);

const client = createClient({
baseUrl: 'https://api.example.com',
fetch: fetchMock,
retry: {
attempts: 3,
retryOn: ['network-error', '5xx', '429'],
retryMethods: ['GET'],
},
validateResponse(data) {
return typeof data === 'object' && data !== null && 'id' in data;
},
});

await expect(client.get('/users/1')).rejects.toBeInstanceOf(ValidationError);

expect(fetchMock).toHaveBeenCalledTimes(1);
});
});
23 changes: 23 additions & 0 deletions packages/client/tests/unit/should-retry.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { HttpError } from '../../src/errors/http-error';
import { NetworkError } from '../../src/errors/network-error';
import { ValidationError } from '../../src/errors/validation-error';
import { RequestAbortedError } from '../../src/errors/request-aborted-error';
import { shouldRetry } from '../../src/core/should-retry';
import type { RetryConfig } from '../../src/types/config';
Expand All @@ -23,6 +24,18 @@ function createHttpError(status: number, statusText = 'Error'): HttpError {
return new HttpError(response);
}

function createValidationError(): ValidationError {
const response = new Response(JSON.stringify({ name: 'Roman' }), {
status: 200,
statusText: 'OK',
headers: {
'content-type': 'application/json',
},
});

return new ValidationError(response, { name: 'Roman' });
}

function createParams(
overrides: Partial<{
attempt: number;
Expand Down Expand Up @@ -131,4 +144,14 @@ describe('shouldRetry', () => {
),
).toBe(false);
});

it('does not retry validation errors', () => {
expect(
shouldRetry(
createParams({
error: createValidationError(),
}),
),
).toBe(false);
});
});
Loading