Skip to content
Closed
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions packages/cli-kit/src/public/node/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/cli-kit/src/public/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,13 @@ export function downloadFile(url: string, to: string): Promise<string> {

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()
Expand Down
Loading