From 24fbc4c5a0f317ac66dc7e3c54f8bffb5c7d5a21 Mon Sep 17 00:00:00 2001 From: "Brandon Waterloo [MSFT]" <36966225+bwateratmsft@users.noreply.github.com> Date: Thu, 12 Sep 2024 09:55:00 -0400 Subject: [PATCH 1/3] Log more in vsdbg script download error case --- src/utils/httpRequest.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/utils/httpRequest.ts b/src/utils/httpRequest.ts index 79b0ca7689..9185e9be7b 100644 --- a/src/utils/httpRequest.ts +++ b/src/utils/httpRequest.ts @@ -130,14 +130,23 @@ export interface ResponseLike { } export async function streamToFile(downloadUrl: string, fileName: string): Promise { - const response = await fetch(downloadUrl); - const writeStream = fse.createWriteStream(fileName); + try { + const response = await fetch(downloadUrl); - for await (const chunk of response.body) { - writeStream.write(chunk); - } + if (!response.ok) { + throw new HttpErrorResponse(response); + } + + const writeStream = fse.createWriteStream(fileName); - writeStream.close(); + for await (const chunk of response.body) { + writeStream.write(chunk); + } + + writeStream.close(); + } catch (error) { + throw new Error(`Failed to download ${downloadUrl}: ${(error as { cause: string }).cause ?? error}`); + } } export function basicAuthHeader(username: string, password: string): string { From 2a0f45f6b43f17123f88e97634e2ea523146a55d Mon Sep 17 00:00:00 2001 From: "Brandon Waterloo [MSFT]" <36966225+bwateratmsft@users.noreply.github.com> Date: Thu, 12 Sep 2024 09:57:22 -0400 Subject: [PATCH 2/3] Better --- src/utils/httpRequest.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/httpRequest.ts b/src/utils/httpRequest.ts index 9185e9be7b..5ad2fb1d2d 100644 --- a/src/utils/httpRequest.ts +++ b/src/utils/httpRequest.ts @@ -145,7 +145,9 @@ export async function streamToFile(downloadUrl: string, fileName: string): Promi writeStream.close(); } catch (error) { - throw new Error(`Failed to download ${downloadUrl}: ${(error as { cause: string }).cause ?? error}`); + // Sometimes the error has a cause field, sometimes a message, sometimes maybe neither + const errorText = (error as { cause: string }).cause ?? (error as { message: string }).message ?? error; + throw new Error(`Failed to download ${downloadUrl}: ${errorText}`); } } From 64bff306c2d04cb11e481adc5b09f3f41ec561f4 Mon Sep 17 00:00:00 2001 From: "Brandon Waterloo [MSFT]" <36966225+bwateratmsft@users.noreply.github.com> Date: Thu, 12 Sep 2024 10:00:56 -0400 Subject: [PATCH 3/3] Make it better yet --- src/utils/httpRequest.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/utils/httpRequest.ts b/src/utils/httpRequest.ts index 5ad2fb1d2d..6c652d1b79 100644 --- a/src/utils/httpRequest.ts +++ b/src/utils/httpRequest.ts @@ -146,7 +146,16 @@ export async function streamToFile(downloadUrl: string, fileName: string): Promi writeStream.close(); } catch (error) { // Sometimes the error has a cause field, sometimes a message, sometimes maybe neither - const errorText = (error as { cause: string }).cause ?? (error as { message: string }).message ?? error; + let errorText: string; + + if (typeof error === 'object') { + errorText = (error as { cause: string }).cause ?? (error as { message: string }).message ?? error.toString(); + } else if (typeof error === 'string') { + errorText = error; + } else { + errorText = error.toString(); + } + throw new Error(`Failed to download ${downloadUrl}: ${errorText}`); } }