Skip to content

Commit

Permalink
fix: retry when PUT request fails (#1601)
Browse files Browse the repository at this point in the history
FML, we have a useless retry function that does not retry the PUT
request when fetch response is non-200.

Not any more.
  • Loading branch information
alanshaw authored Dec 5, 2024
1 parent 8b553a5 commit be2f05f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/upload-client/src/blob/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export async function add(
globalThis.fetch.bind(globalThis)

let fetchDidCallUploadProgressCb = false
const { status } = await retry(
await retry(
async () => {
try {
const res = await fetchWithUploadProgress(address.url, {
Expand All @@ -243,10 +243,13 @@ export async function add(
// @ts-expect-error - this is needed by recent versions of node - see https://github.com/bluesky-social/atproto/pull/470 for more info
duplex: 'half',
})
// do not retry client errors
if (res.status >= 400 && res.status < 500) {
throw new AbortError(`upload failed: ${res.status}`)
}
return res
if (!res.ok) {
throw new Error(`upload failed: ${res.status}`)
}
} catch (err) {
if (options.signal?.aborted === true) {
throw new AbortError('upload aborted')
Expand All @@ -258,7 +261,6 @@ export async function add(
retries: options.retries ?? REQUEST_RETRIES,
}
)
if (status !== 200) throw new Error(`upload failed: ${status}`)

if (!fetchDidCallUploadProgressCb && options.onUploadProgress) {
// the fetch implementation didn't support onUploadProgress
Expand Down
5 changes: 4 additions & 1 deletion packages/upload-client/test/blob.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,10 @@ describe('Blob.add', () => {
)
})

it('throws for bucket URL server error 5xx', async () => {
it('throws for bucket URL server error 5xx', async function () {
// allow time for retries
this.timeout(10_000)

const space = await Signer.generate()
const agent = await Signer.generate()
const bytes = await randomBytes(128)
Expand Down

0 comments on commit be2f05f

Please sign in to comment.