From d73ad7ec8288519d28c3ec95f99952a9d0347b47 Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Wed, 24 Jun 2026 11:48:26 +0000 Subject: [PATCH 1/4] refactor(storage): make onResponse async and improve error formatting for retry failures --- handwritten/storage/src/resumable-upload.ts | 107 +++++++++++++++++-- handwritten/storage/test/resumable-upload.ts | 107 ++++++++++++++++--- 2 files changed, 189 insertions(+), 25 deletions(-) diff --git a/handwritten/storage/src/resumable-upload.ts b/handwritten/storage/src/resumable-upload.ts index 9ebbb6f37a85..3d126c5848be 100644 --- a/handwritten/storage/src/resumable-upload.ts +++ b/handwritten/storage/src/resumable-upload.ts @@ -1345,7 +1345,7 @@ export class Upload extends Writable { }, }; const res = await this.authClient.request(combinedReqOpts); - const successfulRequest = this.onResponse(res); + const successfulRequest = await this.onResponse(res); this.removeListener('error', errorCallback); return successfulRequest ? res : null; @@ -1354,7 +1354,7 @@ export class Upload extends Writable { /** * @return {bool} is the request good? */ - private onResponse(resp: GaxiosResponse) { + private async onResponse(resp: GaxiosResponse) { if ( resp.status !== 200 && this.retryOptions.retryableErrorFn!({ @@ -1363,7 +1363,7 @@ export class Upload extends Writable { name: resp.statusText, }) ) { - this.attemptDelayedRetry(resp); + await this.attemptDelayedRetry(resp); return false; } @@ -1386,9 +1386,7 @@ export class Upload extends Writable { if (retryDelay <= 0) { this.destroy( - new Error( - `Retry total time limit exceeded - ${JSON.stringify(resp.data)}`, - ), + formatRetryError('Retry total time limit exceeded', resp), ); return; } @@ -1409,9 +1407,7 @@ export class Upload extends Writable { } this.numRetries++; } else { - this.destroy( - new Error(`Retry limit exceeded - ${JSON.stringify(resp.data)}`), - ); + this.destroy(formatRetryError('Retry limit exceeded', resp)); } } @@ -1456,6 +1452,99 @@ export class Upload extends Writable { } } +function formatRetryError( + prefix: string, + resp: Pick, +): Error { + const parts: string[] = []; + + if (resp.status !== undefined && !isNaN(resp.status)) { + parts.push(`status: ${resp.status}`); + } + + const err = resp.data; + if (err !== undefined && err !== null) { + if (err instanceof Error) { + const gaxiosErr = err as GaxiosError; + const errParts: string[] = []; + if (gaxiosErr.message) { + errParts.push(gaxiosErr.message); + } + const status = gaxiosErr.status ?? gaxiosErr.response?.status; + if (status !== undefined && !isNaN(status) && status !== resp.status) { + errParts.push(`status: ${status}`); + } + const statusText = gaxiosErr.response?.statusText; + if (statusText) { + errParts.push(`statusText: ${statusText}`); + } + const responseData = gaxiosErr.response?.data; + if (responseData !== undefined && responseData !== null && responseData !== '') { + errParts.push( + `response: ${ + typeof responseData === 'object' + ? JSON.stringify(responseData) + : responseData + }`, + ); + } + if (gaxiosErr.code) { + errParts.push(`code: ${gaxiosErr.code}`); + } + if (errParts.length > 0) { + parts.push(...errParts); + } else { + parts.push(gaxiosErr.toString() || gaxiosErr.name || 'Unknown Error'); + } + } else if (typeof err === 'object') { + const errParts: string[] = []; + const gaxiosErrLike = err as any; + if (gaxiosErrLike.message) { + errParts.push(String(gaxiosErrLike.message)); + } + const status = gaxiosErrLike.status ?? gaxiosErrLike.response?.status; + if (status !== undefined && !isNaN(status) && status !== resp.status) { + errParts.push(`status: ${status}`); + } + const statusText = gaxiosErrLike.response?.statusText; + if (statusText) { + errParts.push(`statusText: ${statusText}`); + } + const responseData = gaxiosErrLike.response?.data; + if (responseData !== undefined && responseData !== null && responseData !== '') { + errParts.push( + `response: ${ + typeof responseData === 'object' + ? JSON.stringify(responseData) + : responseData + }`, + ); + } + if (gaxiosErrLike.code) { + errParts.push(`code: ${String(gaxiosErrLike.code)}`); + } + + if (errParts.length > 0) { + parts.push(...errParts); + } else { + const stringified = JSON.stringify(err); + if (stringified && stringified !== '{}') { + parts.push(stringified); + } + } + } else if (typeof err === 'string') { + if (err !== '') { + parts.push(err); + } + } else { + parts.push(String(err)); + } + } + + const suffix = parts.join(' - '); + return new Error(`${prefix} - ${suffix || 'Unknown Error'}`); +} + export function upload(cfg: UploadConfig) { return new Upload(cfg); } diff --git a/handwritten/storage/test/resumable-upload.ts b/handwritten/storage/test/resumable-upload.ts index 381044d64d9d..eb1ab149350c 100644 --- a/handwritten/storage/test/resumable-upload.ts +++ b/handwritten/storage/test/resumable-upload.ts @@ -2275,10 +2275,10 @@ describe('resumable-upload', () => { describe('500s', () => { const RESP = {status: 500, data: 'error message from server'}; - it('should increase the retry count if less than limit', () => { + it('should increase the retry count if less than limit', async () => { up.getRetryDelay = () => 1; assert.strictEqual(up.numRetries, 0); - assert.strictEqual(up.onResponse(RESP), false); + assert.strictEqual(await up.onResponse(RESP), false); assert.strictEqual(up.numRetries, 1); }); @@ -2287,15 +2287,17 @@ describe('resumable-upload', () => { up.destroy = (err: Error) => { assert.strictEqual( err.message, - `Retry limit exceeded - ${JSON.stringify(RESP.data)}` + `Retry limit exceeded - status: 500 - error message from server`, ); done(); }; - up.onResponse(RESP); - up.onResponse(RESP); - up.onResponse(RESP); - up.onResponse(RESP); + (async () => { + await up.onResponse(RESP); + await up.onResponse(RESP); + await up.onResponse(RESP); + await up.onResponse(RESP); + })().catch(done); }); describe('exponential back off', () => { @@ -2321,19 +2323,19 @@ describe('resumable-upload', () => { assert(delay <= maxTime); // make it keep retrying until the limit is reached - up.onResponse(RESP); + void up.onResponse(RESP); }; up.on('error', (err: Error) => { assert.strictEqual(up.numRetries, 3); assert.strictEqual( err.message, - `Retry limit exceeded - ${JSON.stringify(RESP.data)}` + `Retry limit exceeded - status: 500 - error message from server`, ); done(); }); - up.onResponse(RESP); + void up.onResponse(RESP); clock.runAll(); }); }); @@ -2348,23 +2350,22 @@ describe('resumable-upload', () => { assert.strictEqual(resp, RESP); done(); }); - up.onResponse(RESP); + void up.onResponse(RESP); }); - it('should return true', () => { + it('should return true', async () => { up.getRetryDelay = () => 1; - assert.strictEqual(up.onResponse(RESP), true); + assert.strictEqual(await up.onResponse(RESP), true); }); - it('should handle a custom status code when passed a retry function', () => { + it('should handle a custom status code when passed a retry function', async () => { up.getRetryDelay = () => 1; const RESP = {status: 1000}; const customHandlerFunction = (err: ApiError) => { return err.code === 1000; }; up.retryOptions.retryableErrorFn = customHandlerFunction; - - assert.strictEqual(up.onResponse(RESP), false); + assert.strictEqual(await up.onResponse(RESP), false); }); }); }); @@ -2490,6 +2491,80 @@ describe('resumable-upload', () => { up.attemptDelayedRetry({}); }); + + it('should include correct details for standard native Errors', done => { + up.numRetries = 3; + up.retryLimit = 3; + const nativeError = new Error('native connection issue'); + + up.on('error', (err: Error) => { + assert.strictEqual( + err.message, + 'Retry limit exceeded - native connection issue', + ); + done(); + }); + + up.attemptDelayedRetry({ + status: NaN, + data: nativeError, + }); + }); + + it('should include correct details for custom errors with empty messages', done => { + up.numRetries = 3; + up.retryLimit = 3; + const customError = new Error(''); + (customError as any).code = 'ERR_SOMETHING_SPECIAL'; + + up.on('error', (err: Error) => { + assert.strictEqual( + err.message, + 'Retry limit exceeded - code: ERR_SOMETHING_SPECIAL', + ); + done(); + }); + + up.attemptDelayedRetry({ + status: NaN, + data: customError, + }); + }); + + it('should include correct details for GaxiosErrors with empty/missing response bodies', done => { + up.numRetries = 3; + up.retryLimit = 3; + + const gaxiosError = new GaxiosError( + 'Request failed with status code 429', + { + method: 'POST', + url: 'https://example.com', + } as any, + { + status: 429, + statusText: 'Too Many Requests', + data: '', + config: {}, + headers: {}, + } as any + ); + + up.on('error', (err: Error) => { + // Let's check for the presence of key details rather than an exact string if we are unsure of exact GaxiosError fields, + // or assert the expected string. Let's assert the expected string: + assert(err.message.includes('Retry limit exceeded')); + assert(err.message.includes('Request failed with status code 429')); + assert(err.message.includes('status: 429') || err.message.includes('code: 429')); + assert(err.message.includes('statusText: Too Many Requests')); + done(); + }); + + up.attemptDelayedRetry({ + status: NaN, + data: gaxiosError, + }); + }); }); describe('PROTOCOL_REGEX', () => { From 992c88271849c03866515966e1bb1bdf8bdc0bf9 Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Wed, 24 Jun 2026 12:55:35 +0000 Subject: [PATCH 2/4] refactor(storage): unify error and object formatting in formatRetryError - Unify formatting of Errors and objects under a single block inside formatRetryError to reduce code duplication. - Ensure standard errors, GaxiosErrors, and custom errors with empty/missing properties are correctly formatted. --- handwritten/storage/src/resumable-upload.ts | 42 +++------------------ 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/handwritten/storage/src/resumable-upload.ts b/handwritten/storage/src/resumable-upload.ts index 3d126c5848be..7460811a9216 100644 --- a/handwritten/storage/src/resumable-upload.ts +++ b/handwritten/storage/src/resumable-upload.ts @@ -1458,52 +1458,20 @@ function formatRetryError( ): Error { const parts: string[] = []; - if (resp.status !== undefined && !isNaN(resp.status)) { + if (typeof resp.status === 'number' && !isNaN(resp.status)) { parts.push(`status: ${resp.status}`); } const err = resp.data; if (err !== undefined && err !== null) { - if (err instanceof Error) { - const gaxiosErr = err as GaxiosError; - const errParts: string[] = []; - if (gaxiosErr.message) { - errParts.push(gaxiosErr.message); - } - const status = gaxiosErr.status ?? gaxiosErr.response?.status; - if (status !== undefined && !isNaN(status) && status !== resp.status) { - errParts.push(`status: ${status}`); - } - const statusText = gaxiosErr.response?.statusText; - if (statusText) { - errParts.push(`statusText: ${statusText}`); - } - const responseData = gaxiosErr.response?.data; - if (responseData !== undefined && responseData !== null && responseData !== '') { - errParts.push( - `response: ${ - typeof responseData === 'object' - ? JSON.stringify(responseData) - : responseData - }`, - ); - } - if (gaxiosErr.code) { - errParts.push(`code: ${gaxiosErr.code}`); - } - if (errParts.length > 0) { - parts.push(...errParts); - } else { - parts.push(gaxiosErr.toString() || gaxiosErr.name || 'Unknown Error'); - } - } else if (typeof err === 'object') { - const errParts: string[] = []; + if (typeof err === 'object') { const gaxiosErrLike = err as any; + const errParts: string[] = []; if (gaxiosErrLike.message) { errParts.push(String(gaxiosErrLike.message)); } const status = gaxiosErrLike.status ?? gaxiosErrLike.response?.status; - if (status !== undefined && !isNaN(status) && status !== resp.status) { + if (typeof status === 'number' && !isNaN(status) && status !== resp.status) { errParts.push(`status: ${status}`); } const statusText = gaxiosErrLike.response?.statusText; @@ -1526,6 +1494,8 @@ function formatRetryError( if (errParts.length > 0) { parts.push(...errParts); + } else if (err instanceof Error) { + parts.push(err.toString() || err.name || 'Unknown Error'); } else { const stringified = JSON.stringify(err); if (stringified && stringified !== '{}') { From 6b53f4fac4b391b65305bd1be0d193872bba072c Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Thu, 25 Jun 2026 05:16:37 +0000 Subject: [PATCH 3/4] refactor: rename formatRetryError to buildRetryError for consistency --- handwritten/storage/src/resumable-upload.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handwritten/storage/src/resumable-upload.ts b/handwritten/storage/src/resumable-upload.ts index 7460811a9216..10b496d2d260 100644 --- a/handwritten/storage/src/resumable-upload.ts +++ b/handwritten/storage/src/resumable-upload.ts @@ -1386,7 +1386,7 @@ export class Upload extends Writable { if (retryDelay <= 0) { this.destroy( - formatRetryError('Retry total time limit exceeded', resp), + buildRetryError('Retry total time limit exceeded', resp), ); return; } @@ -1407,7 +1407,7 @@ export class Upload extends Writable { } this.numRetries++; } else { - this.destroy(formatRetryError('Retry limit exceeded', resp)); + this.destroy(buildRetryError('Retry limit exceeded', resp)); } } @@ -1452,7 +1452,7 @@ export class Upload extends Writable { } } -function formatRetryError( +function buildRetryError( prefix: string, resp: Pick, ): Error { From 48432197dbd7ab601b0a9c9182b8beccc4ffafb8 Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Thu, 25 Jun 2026 12:41:23 +0000 Subject: [PATCH 4/4] refactor: convert onResponse and related methods from async to synchronous execution --- handwritten/storage/src/resumable-upload.ts | 6 ++--- handwritten/storage/test/resumable-upload.ts | 28 +++++++++----------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/handwritten/storage/src/resumable-upload.ts b/handwritten/storage/src/resumable-upload.ts index 10b496d2d260..df63ec637b1d 100644 --- a/handwritten/storage/src/resumable-upload.ts +++ b/handwritten/storage/src/resumable-upload.ts @@ -1345,7 +1345,7 @@ export class Upload extends Writable { }, }; const res = await this.authClient.request(combinedReqOpts); - const successfulRequest = await this.onResponse(res); + const successfulRequest = this.onResponse(res); this.removeListener('error', errorCallback); return successfulRequest ? res : null; @@ -1354,7 +1354,7 @@ export class Upload extends Writable { /** * @return {bool} is the request good? */ - private async onResponse(resp: GaxiosResponse) { + private onResponse(resp: GaxiosResponse) { if ( resp.status !== 200 && this.retryOptions.retryableErrorFn!({ @@ -1363,7 +1363,7 @@ export class Upload extends Writable { name: resp.statusText, }) ) { - await this.attemptDelayedRetry(resp); + this.attemptDelayedRetry(resp); return false; } diff --git a/handwritten/storage/test/resumable-upload.ts b/handwritten/storage/test/resumable-upload.ts index eb1ab149350c..a324372f5e27 100644 --- a/handwritten/storage/test/resumable-upload.ts +++ b/handwritten/storage/test/resumable-upload.ts @@ -2275,10 +2275,10 @@ describe('resumable-upload', () => { describe('500s', () => { const RESP = {status: 500, data: 'error message from server'}; - it('should increase the retry count if less than limit', async () => { + it('should increase the retry count if less than limit', () => { up.getRetryDelay = () => 1; assert.strictEqual(up.numRetries, 0); - assert.strictEqual(await up.onResponse(RESP), false); + assert.strictEqual(up.onResponse(RESP), false); assert.strictEqual(up.numRetries, 1); }); @@ -2292,12 +2292,10 @@ describe('resumable-upload', () => { done(); }; - (async () => { - await up.onResponse(RESP); - await up.onResponse(RESP); - await up.onResponse(RESP); - await up.onResponse(RESP); - })().catch(done); + up.onResponse(RESP); + up.onResponse(RESP); + up.onResponse(RESP); + up.onResponse(RESP); }); describe('exponential back off', () => { @@ -2323,7 +2321,7 @@ describe('resumable-upload', () => { assert(delay <= maxTime); // make it keep retrying until the limit is reached - void up.onResponse(RESP); + up.onResponse(RESP); }; up.on('error', (err: Error) => { @@ -2335,7 +2333,7 @@ describe('resumable-upload', () => { done(); }); - void up.onResponse(RESP); + up.onResponse(RESP); clock.runAll(); }); }); @@ -2350,22 +2348,22 @@ describe('resumable-upload', () => { assert.strictEqual(resp, RESP); done(); }); - void up.onResponse(RESP); + up.onResponse(RESP); }); - it('should return true', async () => { + it('should return true', () => { up.getRetryDelay = () => 1; - assert.strictEqual(await up.onResponse(RESP), true); + assert.strictEqual(up.onResponse(RESP), true); }); - it('should handle a custom status code when passed a retry function', async () => { + it('should handle a custom status code when passed a retry function', () => { up.getRetryDelay = () => 1; const RESP = {status: 1000}; const customHandlerFunction = (err: ApiError) => { return err.code === 1000; }; up.retryOptions.retryableErrorFn = customHandlerFunction; - assert.strictEqual(await up.onResponse(RESP), false); + assert.strictEqual(up.onResponse(RESP), false); }); }); });