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

move default retry logic on to defaultOptions #637

Merged
merged 4 commits into from
Oct 24, 2023
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
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface UploadOptions {
onSuccess?: (() => void) | null
onError?: ((error: Error | DetailedError) => void) | null
onShouldRetry?:
| ((error: Error | DetailedError, retryAttempt: number, options: UploadOptions) => boolean)
| ((error: DetailedError, retryAttempt: number, options: UploadOptions) => boolean)
Acconut marked this conversation as resolved.
Show resolved Hide resolved
| null
onUploadUrlAvailable?: (() => void) | null

Expand Down
13 changes: 11 additions & 2 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const defaultOptions = {
addRequestId: false,
onBeforeRequest: null,
onAfterResponse: null,
onShouldRetry: null,
onShouldRetry: defaultOnShouldRetry,

chunkSize: Infinity,
retryDelays: [0, 1000, 3000, 5000],
Expand Down Expand Up @@ -1000,7 +1000,7 @@ function isOnline() {

/**
* Checks whether or not it is ok to retry a request.
* @param {Error} err the error returned from the last request
* @param {Error|DetailedError} err the error returned from the last request
* @param {number} retryAttempt the number of times the request has already been retried
* @param {object} options tus Upload options
*
Expand All @@ -1026,6 +1026,15 @@ function shouldRetry(err, retryAttempt, options) {
return options.onShouldRetry(err, retryAttempt, options)
}

return defaultOnShouldRetry(err)
}

/**
* determines if the request should be retried. Will only retry if not a status 4xx except a 409 or 423
* @param {DetailedError} err
* @returns {boolean}
*/
function defaultOnShouldRetry(err) {
const status = err.originalResponse ? err.originalResponse.getStatus() : 0
return (!inStatusCategory(status, 400) || status === 409 || status === 423) && isOnline()
}
Expand Down
Loading