Skip to content

Commit 98e5c60

Browse files
Sebastian McKenziebestander
authored andcommitted
nicer error message when encountering corrupt tarballs - fixes #132 (#138)
1 parent de2b63d commit 98e5c60

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/fetchers/tarball.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default class TarballFetcher extends BaseFetcher {
5959
if (parts.protocol === null) {
6060
// path to the local tarball
6161
let localTarball;
62+
let isOfflineTarball = false;
6263

6364
let relativeFileLoc = parts.pathname && path.join(this.config.cwd, parts.pathname);
6465
if (relativeFileLoc && await fsUtil.exists(relativeFileLoc)) {
@@ -67,6 +68,7 @@ export default class TarballFetcher extends BaseFetcher {
6768
} else {
6869
// generate a offline cache location
6970
localTarball = path.resolve(this.config.getOfflineMirrorPath(registry, null), ref);
71+
isOfflineTarball = true;
7072
}
7173

7274
if (!(await fsUtil.exists(localTarball))) {
@@ -78,9 +80,25 @@ export default class TarballFetcher extends BaseFetcher {
7880

7981
// flow gets confused with the pipe/on types chain
8082
let cachedStream: Object = fs.createReadStream(localTarball);
83+
84+
let decompressStream = zlib.createUnzip();
85+
86+
// nicer errors for corrupted compressed tarballs
87+
decompressStream.on("error", function (err) {
88+
let msg = `${err.message}. `;
89+
if (isOfflineTarball) {
90+
msg += `Mirror tarball appears to be corrupt. You can resolve this by running:\n\n` +
91+
` $ rm -rf ${localTarball}\n` +
92+
" $ kpm install --save";
93+
} else {
94+
msg += `Error decompressing ${localTarball}, it appears to be corrupt.`;
95+
}
96+
reject(new MessageError(msg));
97+
});
98+
8199
cachedStream
82100
.pipe(validateStream)
83-
.pipe(zlib.createUnzip())
101+
.pipe(decompressStream)
84102
.on("error", reject)
85103
.pipe(extractor);
86104
});

0 commit comments

Comments
 (0)