Skip to content

Commit

Permalink
SwallowedException>ignoredExceptionTypes allow yaml list (#2615)
Browse files Browse the repository at this point in the history
* SwallowedException>ignoredExceptionTypes uses yaml lists

* Update documentation
  • Loading branch information
BraisGabin committed Apr 19, 2020
1 parent 1967898 commit 04d8826
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
6 changes: 5 additions & 1 deletion detekt-cli/src/main/resources/default-detekt-config.yml
Expand Up @@ -174,7 +174,11 @@ exceptions:
ignoreLabeled: false
SwallowedException:
active: false
ignoredExceptionTypes: 'InterruptedException,NumberFormatException,ParseException,MalformedURLException'
ignoredExceptionTypes:
- InterruptedException
- NumberFormatException
- ParseException
- MalformedURLException
allowedExceptionNameRegex: '^(_|(ignore|expected).*)'
ThrowingExceptionFromFinally:
active: false
Expand Down
Expand Up @@ -8,9 +8,9 @@ import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.LazyRegex
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.SplitPattern
import io.gitlab.arturbosch.detekt.rules.ALLOWED_EXCEPTION_NAME
import io.gitlab.arturbosch.detekt.rules.isAllowedExceptionName
import io.gitlab.arturbosch.detekt.rules.valueOrDefaultCommaSeparated
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtCatchClause
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
Expand Down Expand Up @@ -64,7 +64,10 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
* </compliant>
*
* @configuration ignoredExceptionTypes - exception types which should be ignored by this rule
* (default: `'InterruptedException,NumberFormatException,ParseException,MalformedURLException'`)
* (default: `- InterruptedException
* - NumberFormatException
* - ParseException
* - MalformedURLException`)
* @configuration allowedExceptionNameRegex - ignores too generic exception types which match this regex
* (default: `'^(_|(ignore|expected).*)'`)
*/
Expand All @@ -74,13 +77,14 @@ class SwallowedException(config: Config = Config.empty) : Rule(config) {
"The caught exception is swallowed. The original exception could be lost.",
Debt.TWENTY_MINS)

private val ignoredExceptionTypes = SplitPattern(valueOrDefault(IGNORED_EXCEPTION_TYPES, ""))
private val ignoredExceptionTypes = valueOrDefaultCommaSeparated(IGNORED_EXCEPTION_TYPES, emptyList())
.map { it.removePrefix("*").removeSuffix("*") }

private val allowedExceptionNameRegex by LazyRegex(ALLOWED_EXCEPTION_NAME_REGEX, ALLOWED_EXCEPTION_NAME)

override fun visitCatchSection(catchClause: KtCatchClause) {
val exceptionType = catchClause.catchParameter?.typeReference?.text
if (!ignoredExceptionTypes.contains(exceptionType) &&
if (!ignoredExceptionTypes.any { exceptionType?.contains(it, ignoreCase = true) == true } &&
isExceptionSwallowedOrUnused(catchClause) &&
!catchClause.isAllowedExceptionName(allowedExceptionNameRegex)) {
report(CodeSmell(issue, Entity.from(catchClause), issue.description))
Expand Down
@@ -1,7 +1,7 @@
package io.gitlab.arturbosch.detekt.rules.exceptions

import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
Expand Down Expand Up @@ -117,31 +117,33 @@ class SwallowedExceptionSpec : Spek({
assertThat(subject.compileAndLint(code)).hasSize(1)
}

context("ignores given exception types config") {
listOf(listOf("IllegalArgumentException"), "IllegalArgumentException").forEach { ignoredExceptionValue ->
context("ignores given exception types config") {

val config = TestConfig(mapOf(SwallowedException.IGNORED_EXCEPTION_TYPES to "IllegalArgumentException"))
val rule = SwallowedException(config)
val config = TestConfig(SwallowedException.IGNORED_EXCEPTION_TYPES to ignoredExceptionValue)
val rule = SwallowedException(config)

it("ignores given exception type in configuration") {
val code = """
it("ignores given exception type in configuration") {
val code = """
fun f() {
try {
} catch (e: IllegalArgumentException) {
}
}
"""
assertThat(rule.compileAndLint(code)).isEmpty()
}
assertThat(rule.compileAndLint(code)).isEmpty()
}

it("reports exception type that is missing in the configuration") {
val code = """
it("reports exception type that is missing in the configuration") {
val code = """
fun f() {
try {
} catch (e: Exception) {
}
}
"""
assertThat(rule.compileAndLint(code)).hasSize(1)
assertThat(rule.compileAndLint(code)).hasSize(1)
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion docs/pages/documentation/exceptions.md
Expand Up @@ -214,7 +214,10 @@ passed into a newly thrown exception.

#### Configuration options:

* ``ignoredExceptionTypes`` (default: ``'InterruptedException,NumberFormatException,ParseException,MalformedURLException'``)
* ``ignoredExceptionTypes`` (default: ``- InterruptedException
- NumberFormatException
- ParseException
- MalformedURLException``)

exception types which should be ignored by this rule

Expand Down

0 comments on commit 04d8826

Please sign in to comment.