diff --git a/packages/cli-kit/src/public/node/http.test.ts b/packages/cli-kit/src/public/node/http.test.ts index f848c4e76a6..9bb24dd160e 100644 --- a/packages/cli-kit/src/public/node/http.test.ts +++ b/packages/cli-kit/src/public/node/http.test.ts @@ -276,6 +276,24 @@ describe('downloadFile', () => { }) }) + test('Fails if the server returns a 500 error', async () => { + await inTemporaryDirectory(async (tmpDir) => { + // Given + const url = 'https://shopify.example/500.txt' + const filename = '/bin/500.txt' + const to = joinPath(tmpDir, filename) + + // When + const result = downloadFile(url, to) + + // Then + await expect(result).rejects.toThrow( + /Failed to download file from https:\/\/shopify.example\/500.txt. Status: 500/, + ) + await expect(fileExists(to)).resolves.toBe(false) + }) + }) + const runningOnWindows = platformAndArch().platform === 'windows' test.skipIf(runningOnWindows)('Cleans up if download fails', async () => { diff --git a/packages/cli-kit/src/public/node/http.ts b/packages/cli-kit/src/public/node/http.ts index 8cbc8556a57..b214c67eed6 100644 --- a/packages/cli-kit/src/public/node/http.ts +++ b/packages/cli-kit/src/public/node/http.ts @@ -254,7 +254,13 @@ export function downloadFile(url: string, to: string): Promise { nodeFetch(url, {redirect: 'follow'}) .then((res) => { - res.body?.pipe(file) + if (res.ok) { + res.body?.pipe(file) + } else { + file.destroy() + tryToRemoveFile() + reject(new Error(`Failed to download file from ${sanitizedUrl}. Status: ${res.status}`)) + } }) .catch((err) => { tryToRemoveFile()