Skip to content
Myr

Make a non-Error progress-callback throw non-retriable too

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 619c427 · Browse files · 2 files+30 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+30 added−7 removed

  • tests:+24 added−0 removed
  • other:+6 added−7 removed

2 files+30 added−7 removed

modified:source/core/Ky.ts+6 added-7 removed
475 unmodified lines
476 #originalRequest?: Request;477 #requestBodyCanBeCancelled = false;478 readonly #userProvidedAbortSignal: AbortSignal | undefined;479 readonly #beforeRetryHookErrors = new WeakSet<Error>();480 // Errors thrown by a user callback that runs while the request is in flight. Retrying would re-send the body and481 // run the callback again, so they are never retriable, whatever `shouldRetry` says.482 readonly #inFlightCallbackErrors = new WeakSet<Error>();480 // Values thrown by a user callback that runs while the request is in flight. Retrying would re-send the body and481 // run the callback again, so they are never retriable, whatever `shouldRetry` says. A strong `Set` is safe because482 // a `Ky` instance lives for exactly one request, and the callback can throw a non-`Error` value.483 readonly #inFlightCallbackErrors = new Set<unknown>();484 #cachedNormalizedOptions: NormalizedOptions | undefined;485 readonly #startTime: number | undefined;486 #returnedResponseFromBeforeRetryHook = false;487 readonly #responseRequests = new WeakMap<Response, Request>();
211 unmodified lines
699 return Math.min(retry.backoffLimit, jitteredDelay);700 }701702 async #calculateRetryDelay(error: unknown) {702 if (isError(error) && this.#inFlightCallbackErrors.has(error)) {703 if (this.#inFlightCallbackErrors.has(error)) {704 throw error;705 }706707 const retry = normalizeRetryOptions(this.#options.retry);
743 unmodified lines
14511452 // The upload progress wrapper errors the request body stream when its callback throws, which the runtime reports as a network failure. Surface the callback error instead.1453 const progressCallbackError = getProgressCallbackError(this.#originalRequest?.body ?? undefined);1454 if (progressCallbackError !== undefined) {1454 if (isError(progressCallbackError)) {1455 this.#inFlightCallbackErrors.add(progressCallbackError);1455 this.#inFlightCallbackErrors.add(progressCallbackError);1456 }14561457 // eslint-disable-next-line @typescript-eslint/only-throw-error -- The callback can throw any value, and non-Error throws are propagated as-is elsewhere.1458 throw progressCallbackError;1459 }
96 unmodified lines
modified:test/stream.ts+24 added
1517 unmodified lines
1518 t.is(getRequestCount(), 0);1519 t.is(callbackCalls, 1);1520 t.is(beforeRetryCalls, 0);1521});15221523// The marker only accepted `Error` values, so a callback that threw a string was still retried even though a callback that threw an `Error` was not. A `Ky` instance lives for one request, so a strong `Set` is safe.1524test('a non-Error throw from an upload progress callback is not retried either', async t => {1525 const {server, getRequestCount} = await createUploadProgressTestServer(t);1526 let callbackCalls = 0;15271528 try {1529 await ky.post(server.url, {1530 body: 'x'.repeat(1024),1531 retry: {limit: 3, methods: ['post'], delay: () => 0, shouldRetry: () => true},1532 onUploadProgress() {1533 callbackCalls++;1534 // eslint-disable-next-line @typescript-eslint/only-throw-error1535 throw 'upload progress failed';1536 },1537 }).text();1538 t.fail('should have thrown');1539 } catch (error) {1540 t.is(error, 'upload progress failed');1541 }15421543 t.is(getRequestCount(), 0);1544 t.is(callbackCalls, 1);1545});