Skip to content

Commit 9ea29bc

Browse files
committed
no using async in a callback
Signed-off-by: Jeromy Cannon <[email protected]>
1 parent 98ddc38 commit 9ea29bc

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

src/core/k8.mjs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -608,44 +608,64 @@ export class K8 {
608608
async ({ status }) => {
609609
if (status === 'Failure') {
610610
self._deleteTempFile(tmpFile)
611-
return reject(new FullstackTestingError('Tar command failed with status Failure'))
611+
const errorMessage = `tar command failed with status Failure while copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`
612+
this.logger.error(errorMessage)
613+
return reject(new FullstackTestingError(errorMessage))
612614
}
613615
this.logger.debug(`copyFrom.callback(status)=${status}`)
614616
})
615617
.then(conn => {
616-
conn.on('close', async (code, reason) => {
618+
conn.on('close', (code, reason) => {
619+
this.logger.debug(`connection closed copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
617620
if (code !== 1000) { // code 1000 is the success code
618-
return reject(new FullstackTestingError(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} because of error (${code}): ${reason}`))
621+
const errorMessage = `failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} because of error (${code}): ${reason}`
622+
this.logger.error(errorMessage)
623+
return reject(new FullstackTestingError(errorMessage))
619624
}
620625

621626
outputFileStream.end()
622-
outputFileStream.close(async () => {
627+
let rejection
628+
outputFileStream.close(() => {
623629
this.logger.debug(`finished closing writerStream copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
624630

625631
try {
626632
// extract the downloaded file
627-
await tar.x({
633+
tar.x({
628634
file: tmpFile,
629635
cwd: destDir
630-
})
631-
self._deleteTempFile(tmpFile)
636+
}).then(() => {
637+
self._deleteTempFile(tmpFile)
632638

633-
const stat = fs.statSync(destPath)
634-
if (stat && stat.size === srcFileSize) {
635-
return resolve(true)
636-
} else {
637-
if (!stat) {
638-
additionalErrorMessageDetail = ', statSync returned no file status for the destination file'
639+
const stat = fs.statSync(destPath)
640+
if (stat && stat.size === srcFileSize) {
641+
this.logger.info(`Finished successfully copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
639642
} else {
640-
additionalErrorMessageDetail = `, stat.size=${stat.size} != srcFileSize=${srcFileSize}`
643+
rejection = true
644+
if (!stat) {
645+
additionalErrorMessageDetail = ', statSync returned no file status for the destination file'
646+
} else {
647+
additionalErrorMessageDetail = `, stat.size=${stat.size} != srcFileSize=${srcFileSize}`
648+
}
641649
}
642-
}
650+
}).catch((err) => {
651+
this.logger.error(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to download file completely: ${destPath}${additionalErrorMessageDetail}`, err)
652+
rejection = true
653+
})
643654
} catch (e) {
644-
return reject(new FullstackTestingError(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to extract file: ${destPath}`, e))
655+
const errorMessage = `failed to complete copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to extract file: ${destPath}`
656+
this.logger.error(errorMessage, e)
657+
rejection = true
658+
throw new FullstackTestingError(errorMessage, e)
645659
}
646660
})
647661

648-
return reject(new FullstackTestingError(`failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to download file completely: ${destPath}${additionalErrorMessageDetail}`))
662+
if (rejection) {
663+
const errorMessage = `failed copying from ${podName}:${srcDir}/${srcFile} to ${destPath} to download file completely: ${destPath}${additionalErrorMessageDetail}`
664+
this.logger.error(errorMessage)
665+
return reject(new FullstackTestingError(errorMessage))
666+
} else {
667+
return resolve(true)
668+
}
649669
})
650670

651671
conn.on('error', (e) => {
@@ -656,16 +676,19 @@ export class K8 {
656676
})
657677

658678
errStream.on('data', (data) => {
659-
this.logger.error(`error encountered copying from ${podName}:${srcDir}/${srcFile} to ${destPath}, error: ${data.toString()}`)
660-
return reject(new FullstackTestingError(`error encountered copying from ${podName}:${srcDir}/${srcFile} to ${destPath}, error: ${data.toString()}`))
679+
const errorMessage = `error encountered copying from ${podName}:${srcDir}/${srcFile} to ${destPath}, error: ${data.toString()}`
680+
this.logger.error(errorMessage)
681+
return reject(new FullstackTestingError(errorMessage))
661682
})
662683

663684
outputFileStream.on('close', () => {
664685
this.logger.debug(`finished copying from ${podName}:${srcDir}/${srcFile} to ${destPath}`)
665686
})
666687

667688
outputFileStream.on('error', (err) => {
668-
return reject(new FullstackTestingError(`writerStream error encountered copying from ${podName}:${srcDir}/${srcFile} to ${destPath}, err: ${err.toString()}`, err))
689+
const errorMessage = `writerStream error encountered copying from ${podName}:${srcDir}/${srcFile} to ${destPath}, err: ${err.toString()}`
690+
this.logger.error(errorMessage, err)
691+
return reject(new FullstackTestingError(errorMessage, err))
669692
})
670693

671694
outputFileStream.on('end', () => {
@@ -685,8 +708,9 @@ export class K8 {
685708
})
686709
})
687710
} catch (e) {
688-
throw new FullstackTestingError(
689-
`failed to download file from ${podName}:${containerName} [${srcPath} -> ${destDir}]: ${e.message}`, e)
711+
const errorMessage = `failed to download file from ${podName}:${containerName} [${srcPath} -> ${destDir}]: ${e.message}`
712+
this.logger.error(errorMessage, e)
713+
throw new FullstackTestingError(errorMessage, e)
690714
}
691715
}
692716

0 commit comments

Comments
 (0)