Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception message #4823

Merged
merged 4 commits into from Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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