mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Improve exception handling
This commit is contained in:
@@ -352,7 +352,7 @@ public class OdasaOutput {
|
||||
currentSpecFileEntry.getTrapFolder().toPath().resolve(dep));
|
||||
}
|
||||
|
||||
public void closeWithoutAdditionalFiles() {
|
||||
public void setHasError() {
|
||||
hasError = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user