Improve exception handling

This commit is contained in:
Tamas Vajk
2022-01-21 11:40:06 +01:00
committed by Ian Lynagh
parent 29f4eb96e1
commit 82fe08ea8e
3 changed files with 30 additions and 26 deletions

View File

@@ -352,7 +352,7 @@ public class OdasaOutput {
currentSpecFileEntry.getTrapFolder().toPath().resolve(dep));
}
public void closeWithoutAdditionalFiles() {
public void setHasError() {
hasError = true;
}
}

View File

@@ -33,9 +33,11 @@ class ExternalClassExtractor(val logger: FileLogger, val invocationTrapFile: Str
logger.info("Skipping extracting class ${irClass.name}")
} else {
val trapFile = manager.file
val trapTmpFile = File.createTempFile("${trapFile.nameWithoutExtension}.", ".${trapFile.extension}.tmp", trapFile.parentFile)
val binaryPath = getIrClassBinaryPath(irClass)
try {
GZIPOutputStream(trapFile.outputStream()).bufferedWriter().use { trapFileBW ->
GZIPOutputStream(trapTmpFile.outputStream()).bufferedWriter().use { trapFileBW ->
// We want our comments to be the first thing in the file,
// so start off with a mere TrapWriter
val tw = TrapWriter(TrapLabelManager(), trapFileBW)
@@ -57,9 +59,14 @@ class ExternalClassExtractor(val logger: FileLogger, val invocationTrapFile: Str
fileExtractor.extractClassSource(irClass)
}
if (!trapTmpFile.renameTo(trapFile)) {
logger.warn(Severity.Error, "Failed to rename $trapTmpFile to $trapFile")
}
} catch (e: Exception) {
manager.closeWithoutAdditionalFiles()
trapFile.delete()
manager.setHasError()
trapTmpFile.delete()
logger.error("Failed to extract '$binaryPath'", e)
}
}

View File

@@ -114,32 +114,24 @@ fun doFile(invocationTrapFile: String,
if (checkTrapIdentical || !trapFile.exists()) {
val trapTmpFile = File.createTempFile("$srcFilePath.", ".trap.tmp", trapFileDir)
var hasError = false
trapTmpFile.bufferedWriter().use { trapFileBW ->
// We want our comments to be the first thing in the file,
// so start off with a mere TrapWriter
val tw = TrapWriter(TrapLabelManager(), trapFileBW)
tw.writeComment("Generated by the CodeQL Kotlin extractor for kotlin source code")
tw.writeComment("Part of invocation $invocationTrapFile")
// Now elevate to a SourceFileTrapWriter, and populate the
// file information
val sftw = tw.makeSourceFileTrapWriter(srcFile, true)
val externalClassExtractor = ExternalClassExtractor(logger, invocationTrapFile, srcFilePath, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
val fileExtractor = KotlinFileExtractor(logger, sftw, srcFilePath, null, externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
try {
try {
trapTmpFile.bufferedWriter().use { trapFileBW ->
// We want our comments to be the first thing in the file,
// so start off with a mere TrapWriter
val tw = TrapWriter(TrapLabelManager(), trapFileBW)
tw.writeComment("Generated by the CodeQL Kotlin extractor for kotlin source code")
tw.writeComment("Part of invocation $invocationTrapFile")
// Now elevate to a SourceFileTrapWriter, and populate the
// file information
val sftw = tw.makeSourceFileTrapWriter(srcFile, true)
val externalClassExtractor = ExternalClassExtractor(logger, invocationTrapFile, srcFilePath, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
val fileExtractor = KotlinFileExtractor(logger, sftw, srcFilePath, null, externalClassExtractor, primitiveTypeMapping, pluginContext, genericSpecialisationsExtracted)
fileExtractor.extractFileContents(srcFile, sftw.fileId)
externalClassExtractor.extractExternalClasses()
} catch (e: Exception) {
hasError = true
logger.error("Failed to extract '$srcFilePath'", e)
}
}
if (hasError) {
if (!trapTmpFile.delete()) {
logger.warn(Severity.WarnLow, "Failed to delete $trapTmpFile")
}
} else {
if (checkTrapIdentical && trapFile.exists()) {
if (equivalentTrap(trapTmpFile, trapFile)) {
if (!trapTmpFile.delete()) {
@@ -158,6 +150,11 @@ fun doFile(invocationTrapFile: String,
logger.warn(Severity.WarnLow, "Failed to rename $trapTmpFile to $trapFile")
}
}
} catch (e: Exception) {
logger.error("Failed to extract '$srcFilePath'", e)
if (!trapTmpFile.delete()) {
logger.warn(Severity.WarnLow, "Failed to delete $trapTmpFile")
}
}
}
}