Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions apps/studio/src/modules/sync/lib/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import {
import { isErrnoException } from '@studio/common/lib/is-errno-exception';
import { getCurrentUserId } from '@studio/common/lib/shared-config';
import { fetchSyncableSites } from '@studio/common/lib/sync/sync-api';
import { shouldRetryTusStatus } from '@studio/common/lib/sync/tus-upload';
import wpcomFactory from '@studio/common/lib/wpcom-factory';
import wpcomXhrRequest from '@studio/common/lib/wpcom-xhr-request-factory';
import { SyncSite } from '@studio/common/types/sync';
import { __, sprintf } from '@wordpress/i18n';
import { Upload } from 'tus-js-client';
import { z } from 'zod';
import {
Expand Down Expand Up @@ -319,12 +321,7 @@ export async function pushArchive(
}

const status = error.originalResponse ? error.originalResponse.getStatus() : 0;
// Stop retrying if the upload failed because of a 403 error.
if ( status === 403 ) {
return false;
}

return true;
return shouldRetryTusStatus( status );
},
} );

Expand Down Expand Up @@ -388,8 +385,38 @@ export async function pushArchive(
return { success: false, error: parseResult.data.error };
}

return { success: false, error: 'Unknown error' };
// A bare upload failure (e.g. a 413) has no `{ error: string }` body, so
// fall back to the HTTP status for a meaningful message.
const status = getTusErrorStatus( error );
if ( status === 413 ) {
return {
success: false,
error: __( 'The site archive is too large to upload right now. Please try again later.' ),
};
}
if ( status ) {
return {
success: false,
// translators: %d is the HTTP status code of the failed upload, e.g. 500.
error: sprintf( __( 'Upload failed with HTTP status %d.' ), status ),
};
}

return { success: false, error: __( 'Unknown error' ) };
}
}

function getTusErrorStatus( error: unknown ): number {
if (
typeof error === 'object' &&
error !== null &&
'originalResponse' in error &&
error.originalResponse &&
typeof ( error.originalResponse as { getStatus?: unknown } ).getStatus === 'function'
) {
return ( error.originalResponse as { getStatus: () => number } ).getStatus();
}
return 0;
}

export async function downloadSyncBackup(
Expand Down
13 changes: 12 additions & 1 deletion tools/common/lib/sync/tus-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import fs from 'fs';
import nodePath from 'path';
import { Upload } from 'tus-js-client';

/**
* 5xx and network errors (status 0) are transient. 4xx are terminal, except 409
* (Conflict) and 423 (Locked), which the resumable protocol uses transiently.
*/
export function shouldRetryTusStatus( status: number ): boolean {
if ( status >= 400 && status < 500 ) {
return status === 409 || status === 423;
}
return true;
}

export type TusUploadOptions = {
token: string;
remoteSiteId: number;
Expand Down Expand Up @@ -110,7 +121,7 @@ export function createTusUpload( options: TusUploadOptions ): {
}

const status = error.originalResponse ? error.originalResponse.getStatus() : 0;
return status !== 403;
return shouldRetryTusStatus( status );
},
} );

Expand Down