Skip to content

Commit

Permalink
Refactor to use IllegalStateException
Browse files Browse the repository at this point in the history
  • Loading branch information
Chao Zhang committed Nov 2, 2020
1 parent 2bd3f7d commit a10a484
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.gitlab.arturbosch.detekt.core

import io.github.detekt.psi.absolutePath
import io.github.detekt.tooling.api.UnexpectedError
import io.github.detekt.tooling.api.spec.ProcessingSpec
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.FileProcessListener
Expand Down Expand Up @@ -70,7 +69,7 @@ internal class Analyzer(
ktFiles.map { file ->
processors.forEach { it.onProcess(file, bindingContext) }
val findings = runCatching { analyze(file, bindingContext, compilerResources) }
.onFailure { throw UnexpectedError(message = createErrorMessage(file, it), cause = it) }
.onFailure { throwIllegalStateException(file, it) }
.getOrDefault(emptyMap())
processors.forEach { it.onProcessComplete(file, findings, bindingContext) }
findings
Expand All @@ -88,9 +87,7 @@ internal class Analyzer(
val findings = analyze(file, bindingContext, compilerResources)
processors.forEach { it.onProcessComplete(file, findings, bindingContext) }
findings
}.recover {
throw UnexpectedError(message = createErrorMessage(file, it), cause = it)
}
}.recover { throwIllegalStateException(file, it) }
}
return awaitAll(tasks).filterNotNull()
}
Expand Down Expand Up @@ -144,12 +141,15 @@ private fun MutableMap<String, List<Finding>>.mergeSmells(other: Map<String, Lis
}
}

private fun createErrorMessage(file: KtFile, error: Throwable): String = """
private fun throwIllegalStateException(file: KtFile, error: Throwable): Nothing {
val message = """
Analyzing ${file.absolutePath()} led to an exception.
The original exception message was: ${error.localizedMessage}
Running detekt '${whichDetekt() ?: "unknown"}' on Java '${whichJava()}' on OS '${whichOS()}'
If the exception message does not help, please feel free to create an issue on our GitHub page.
""".trimIndent()
""".trimIndent()
throw IllegalStateException(message, error)
}

internal fun ProcessingSpec.workaroundConfiguration(config: Config): Config = with(configSpec) {
var declaredConfig: Config? = when {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.gitlab.arturbosch.detekt.core

import io.github.detekt.test.utils.compileForTest
import io.github.detekt.tooling.api.UnexpectedError
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
Expand Down Expand Up @@ -38,7 +37,7 @@ class AnalyzerSpec : Spek({

assertThatThrownBy {
settings.use { analyzer.run(listOf(compileForTest(testFile))) }
}.isInstanceOf(UnexpectedError::class.java)
}.isInstanceOf(IllegalStateException::class.java)
}

it("throw error explicitly in parallel when config has wrong value in config") {
Expand All @@ -57,7 +56,7 @@ class AnalyzerSpec : Spek({

assertThatThrownBy { settings.use { analyzer.run(listOf(compileForTest(testFile))) } }
.isInstanceOf(CompletionException::class.java)
.hasCauseInstanceOf(UnexpectedError::class.java)
.hasCauseInstanceOf(IllegalStateException::class.java)
}
}
})
Expand Down

0 comments on commit a10a484

Please sign in to comment.