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 1 commit
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 @@ -153,8 +153,13 @@ private fun MutableMap<String, List<Finding>>.mergeSmells(other: Map<String, Lis
}

private fun throwIllegalStateException(file: KtFile, error: Throwable): Nothing {
val location = error.stackTrace.firstOrNull()?.let {
"${it.fileName} => ${it.className} => ${it.methodName} => ${it.lineNumber}."
VitalyVPinchuk marked this conversation as resolved.
Show resolved Hide resolved
} ?: "Unknown."
VitalyVPinchuk marked this conversation as resolved.
Show resolved Hide resolved
VitalyVPinchuk marked this conversation as resolved.
Show resolved Hide resolved

val message = """
Analyzing ${file.absolutePath()} led to an exception.
Location: $location
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,20 @@ 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(FaultyRule::class.java.name)
}
}
}

Expand All @@ -106,3 +120,15 @@ 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) {
error("Deliberately triggered error.")
}
}
Expand Up @@ -2,3 +2,5 @@ style:
MaxLineLength:
active: true
maxLineLength: 120
FaultyRule:
active: true