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

Log more in vsdbg script download error case #4383

Merged
merged 3 commits into from
Sep 12, 2024
Merged
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
32 changes: 26 additions & 6 deletions src/utils/httpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,34 @@ export interface ResponseLike {
}

export async function streamToFile(downloadUrl: string, fileName: string): Promise<void> {
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);

for await (const chunk of response.body) {
writeStream.write(chunk);
}

writeStream.close();
writeStream.close();
} catch (error) {
// Sometimes the error has a cause field, sometimes a message, sometimes maybe neither
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}`);
}
}

export function basicAuthHeader(username: string, password: string): string {
Expand Down
Loading