Skip to content

Commit d4fc0c3

Browse files
committed
claude normalizes tar error messages
1 parent 7f001f4 commit d4fc0c3

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

src/extract.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,39 @@ import { mkdir } from 'node:fs/promises'
33
import { platform } from 'node:os'
44
import { resolve } from 'node:path'
55

6+
/**
7+
* Normalizes tar error messages across different platforms
8+
*/
9+
function normalizeTarError(stderr: string): string {
10+
const errorMessage = stderr.trim()
11+
12+
// File not found errors
13+
if (
14+
errorMessage.includes('Cannot open') ||
15+
errorMessage.includes('Failed to open') ||
16+
errorMessage.includes('No such file or directory')
17+
) {
18+
return 'tar command failed: File not found'
19+
}
20+
21+
// Invalid archive format errors
22+
if (
23+
errorMessage.includes('Unrecognized archive format') ||
24+
errorMessage.includes('Error opening archive') ||
25+
errorMessage.includes('incorrect header check')
26+
) {
27+
return 'tar command failed: Invalid archive format'
28+
}
29+
30+
// General extraction errors
31+
if (errorMessage.includes('Error is not recoverable') || errorMessage.includes('Child returned status')) {
32+
return 'tar command failed: Extraction error'
33+
}
34+
35+
// If no specific pattern matches, return a generic message
36+
return `tar command failed: ${errorMessage}`
37+
}
38+
639
export async function extractTarGz(archivePath: string, outputDir: string): Promise<void> {
740
await mkdir(outputDir, { recursive: true })
841

@@ -21,8 +54,8 @@ export async function extractTarGz(archivePath: string, outputDir: string): Prom
2154
if (code === 0) {
2255
resolve()
2356
} else {
24-
// Include stderr in error message for better debugging
25-
const errorMsg = stderr.trim() || `${tarCmd} command failed with code ${code}`
57+
// Normalize error message for consistent cross-platform behavior
58+
const errorMsg = stderr.trim() ? normalizeTarError(stderr) : `${tarCmd} command failed with code ${code}`
2659
reject(new Error(errorMsg))
2760
}
2861
})

test/extract.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ describe('Archive Extraction', () => {
1919
await extractArchive(archivePath, testDir)
2020
} catch (error) {
2121
// Expected to fail with gzip/tar error from our native implementation
22-
assert.match(
23-
(error as Error).message,
24-
/tar command failed|Failed to extract|incorrect header check|Error opening archive/,
25-
)
22+
assert.match((error as Error).message, /tar command failed: Invalid archive format/)
2623
}
2724

2825
// Clean up

test/tar-extraction.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('Native tar.gz Extraction Edge Cases', () => {
4343
try {
4444
await assert.rejects(
4545
async () => await extractTarGz(archivePath, testDir),
46-
/No files extracted from archive|tar command failed|Error opening archive/,
46+
/tar command failed: Invalid archive format/,
4747
)
4848
} finally {
4949
await rm(testDir, { recursive: true }).catch(() => {})
@@ -67,7 +67,7 @@ describe('Native tar.gz Extraction Edge Cases', () => {
6767
try {
6868
await assert.rejects(
6969
async () => await extractTarGz(archivePath, testDir),
70-
/No files extracted from archive|tar command failed|Error opening archive/,
70+
/tar command failed: Invalid archive format/,
7171
)
7272
} finally {
7373
await rm(testDir, { recursive: true }).catch(() => {})
@@ -80,7 +80,7 @@ describe('Native tar.gz Extraction Edge Cases', () => {
8080

8181
await assert.rejects(
8282
async () => await extractTarGz(nonExistentPath, outputDir),
83-
/tar command failed|Failed to open/,
83+
/tar command failed: File not found/,
8484
)
8585
})
8686

0 commit comments

Comments
 (0)