Skip to content

Commit

Permalink
Improve exception message (#4823)
Browse files Browse the repository at this point in the history
* Improve exception message

Report detailed location of an error.

* Add test case

* Split tests

* Improve error location message presentation
  • Loading branch information
VitalyVPinchuk committed Jun 1, 2022
1 parent db0b4b5 commit fe03b6d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Expand Up @@ -155,6 +155,7 @@ private fun MutableMap<String, List<Finding>>.mergeSmells(other: Map<String, Lis
private fun throwIllegalStateException(file: KtFile, error: Throwable): Nothing {
val message = """
Analyzing ${file.absolutePath()} led to an exception.
Location: ${error.stackTrace.firstOrNull()?.toString()}
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.
Expand Down
Expand Up @@ -86,6 +86,34 @@ class AnalyzerSpec {

assertThat(settings.use { analyzer.run(listOf(compileForTest(testFile))) }).isEmpty()
}

@Test
fun `with faulty rule`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(
testFile,
yamlConfig("configs/config-value-type-correct.yml")
)
val analyzer = Analyzer(settings, listOf(FaultyRuleSetProvider()), emptyList())

assertThatThrownBy { settings.use { analyzer.run(listOf(compileForTest(testFile))) } }
.hasCauseInstanceOf(IllegalStateException::class.java)
.hasMessageContaining("Location: ${FaultyRule::class.java.name}")
}

@Test
fun `with faulty rule without stack trace`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(
testFile,
yamlConfig("configs/config-value-type-correct.yml")
)
val analyzer = Analyzer(settings, listOf(FaultyRuleNoStackTraceSetProvider()), emptyList())

assertThatThrownBy { settings.use { analyzer.run(listOf(compileForTest(testFile))) } }
.hasCauseInstanceOf(IllegalStateException::class.java)
.hasMessageContaining("Location: ${null}")
}
}
}

Expand All @@ -106,3 +134,29 @@ private class MaxLineLength(config: Config, threshold: Int?) : Rule(config) {
}
}
}

private class FaultyRuleSetProvider : RuleSetProvider {
override val ruleSetId: String = "style"
override fun instance(config: Config) = RuleSet(ruleSetId, listOf(FaultyRule(config)))
}

private class FaultyRule(config: Config) : Rule(config) {
override val issue = Issue(this::class.java.simpleName, Severity.Style, "", Debt.FIVE_MINS)
override fun visitKtFile(file: KtFile) {
throw object : IllegalStateException("Deliberately triggered error.") {}
}
}

private class FaultyRuleNoStackTraceSetProvider : RuleSetProvider {
override val ruleSetId: String = "style"
override fun instance(config: Config) = RuleSet(ruleSetId, listOf(FaultyRuleNoStackTrace(config)))
}

private class FaultyRuleNoStackTrace(config: Config) : Rule(config) {
override val issue = Issue(this::class.java.simpleName, Severity.Style, "", Debt.FIVE_MINS)
override fun visitKtFile(file: KtFile) {
throw object : IllegalStateException("Deliberately triggered error without stack trace.") {
init { stackTrace = emptyArray() }
}
}
}
Expand Up @@ -2,3 +2,7 @@ style:
MaxLineLength:
active: true
maxLineLength: 120
FaultyRule:
active: true
FaultyRuleNoStackTrace:
active: true

0 comments on commit fe03b6d

Please sign in to comment.