fix: error code when cannot add tarball to store (#7694)

close #7679
This commit is contained in:
Zoltan Kochan
2024-02-23 10:36:22 +01:00
committed by GitHub
parent 9fdfaaf4ae
commit 3ded840001
6 changed files with 34 additions and 19 deletions

View File

@@ -0,0 +1,8 @@
---
"@pnpm/tarball-fetcher": patch
"@pnpm/error": patch
"@pnpm/worker": patch
"pnpm": patch
---
Print the right error code when a package fails to be added to the store [#7679](https://github.com/pnpm/pnpm/issues/7679).

View File

@@ -99,6 +99,7 @@ export function createDownloader (
})
async function fetch (currentAttempt: number): Promise<FetchResult> {
let data: Buffer
try {
const res = await fetchFromRegistry(url, {
authHeaderValue,
@@ -142,26 +143,26 @@ export function createDownloader (
})
}
const data: Buffer = Buffer.from(new SharedArrayBuffer(downloaded))
data = Buffer.from(new SharedArrayBuffer(downloaded))
let offset: number = 0
for (const chunk of chunks) {
chunk.copy(data, offset)
offset += chunk.length
}
return await addFilesFromTarball({
buffer: data,
cafsDir: opts.cafs.cafsDir,
readManifest: opts.readManifest,
integrity: opts.integrity,
filesIndexFile: opts.filesIndexFile,
url,
pkg: opts.pkg,
})
} catch (err: any) { // eslint-disable-line
err.attempts = currentAttempt
err.resource = url
throw err
}
return addFilesFromTarball({
buffer: data,
cafsDir: opts.cafs.cafsDir,
readManifest: opts.readManifest,
integrity: opts.integrity,
filesIndexFile: opts.filesIndexFile,
url,
pkg: opts.pkg,
})
}
}
}

View File

@@ -484,7 +484,7 @@ test('fail when extracting a broken tarball', async () => {
lockfileDir: process.cwd(),
pkg: {},
})
).rejects.toThrow(`Failed to unpack the tarball from "${registry}foo.tgz": Error: Invalid checksum for TAR header at offset 0. Expected 0, got NaN`
).rejects.toThrow(`Failed to add tarball from "${registry}foo.tgz" to store: Invalid checksum for TAR header at offset 0. Expected 0, got NaN`
)
expect(scope.isDone()).toBeTruthy()
})

View File

@@ -15,7 +15,7 @@ export class PnpmError extends Error {
}
) {
super(message)
this.code = `ERR_PNPM_${code}`
this.code = code.startsWith('ERR_PNPM_') ? code : `ERR_PNPM_${code}`
this.hint = opts?.hint
this.attempts = opts?.attempts
}

View File

@@ -59,7 +59,7 @@ export async function addFilesFromDir (
localWorker.once('message', ({ status, error, value }) => {
workerPool!.checkinWorker(localWorker)
if (status === 'error') {
reject(new PnpmError('GIT_FETCH_FAILED', error as string))
reject(new PnpmError(error.code ?? 'GIT_FETCH_FAILED', error.message as string))
return
}
resolve(value)
@@ -131,7 +131,7 @@ export async function addFilesFromTarball (
}))
return
}
reject(new PnpmError('TARBALL_EXTRACT', `Failed to unpack the tarball from "${opts.url}": ${error as string}`))
reject(new PnpmError(error.code ?? 'TARBALL_EXTRACT', `Failed to add tarball from "${opts.url}" to store: ${error.message as string}`))
return
}
resolve(value)
@@ -162,7 +162,7 @@ export async function readPkgFromCafs (
localWorker.once('message', ({ status, error, value }) => {
workerPool!.checkinWorker(localWorker)
if (status === 'error') {
reject(new PnpmError('READ_FROM_STORE', error as string))
reject(new PnpmError(error.code ?? 'READ_FROM_STORE', error.message as string))
return
}
resolve(value)
@@ -188,7 +188,7 @@ export async function importPackage (
localWorker.once('message', ({ status, error, value }: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any
workerPool!.checkinWorker(localWorker)
if (status === 'error') {
reject(new PnpmError('LINKING_FAILED', error as string))
reject(new PnpmError(error.code ?? 'LINKING_FAILED', error.message as string))
return
}
resolve(value)
@@ -211,7 +211,7 @@ export async function symlinkAllModules (
localWorker.once('message', ({ status, error, value }: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any
workerPool!.checkinWorker(localWorker)
if (status === 'error') {
reject(new PnpmError('SYMLINK_FAILED', error as string))
reject(new PnpmError(error.code ?? 'SYMLINK_FAILED', error.message as string))
return
}
resolve(value)
@@ -232,7 +232,7 @@ export async function hardLinkDir (src: string, destDirs: string[]): Promise<voi
localWorker.once('message', ({ status, error }: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any
workerPool!.checkinWorker(localWorker)
if (status === 'error') {
reject(new PnpmError('HARDLINK_FAILED', error as string))
reject(new PnpmError(error.code ?? 'HARDLINK_FAILED', error.message as string))
return
}
resolve()

View File

@@ -103,7 +103,13 @@ async function handleMessage (
}
}
} catch (e: any) { // eslint-disable-line
parentPort!.postMessage({ status: 'error', error: e.toString() })
parentPort!.postMessage({
status: 'error',
error: {
code: e.code,
message: e.message ?? e.toString(),
},
})
}
}