From 2dad2ec4d8b6ce112d4e7d64ca9c0664289c64cf Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Thu, 18 Jul 2024 00:40:36 +0200 Subject: [PATCH] Fix incorrect creation of `CommandSyntaxException` with cause Previously `initCause` was failing because `CommandSyntaxException` is created by Brigadier in a way which does not permit setting a cause. Therefore, now instead add the cause exception as 'suppressed'. --- .../commands/ListUserActivityCommand.kt | 2 +- .../modules/commands/RemoveContentCommand.kt | 2 +- .../commands/ListUserActivityCommandTest.kt | 24 +++++++++++++++++-- .../commands/RemoveContentCommandTest.kt | 22 +++++++++++++++++ 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/io/github/mojira/arisa/modules/commands/ListUserActivityCommand.kt b/src/main/kotlin/io/github/mojira/arisa/modules/commands/ListUserActivityCommand.kt index 41079168..f314f50f 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/commands/ListUserActivityCommand.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/commands/ListUserActivityCommand.kt @@ -28,7 +28,7 @@ class ListUserActivityCommand( is Either.Left -> throw CommandExceptions.CANNOT_QUERY_USER_ACTIVITY .create(sanitizedUserName, jql) - .initCause(either.a) + .apply { addSuppressed(either.a) } is Either.Right -> either.b } diff --git a/src/main/kotlin/io/github/mojira/arisa/modules/commands/RemoveContentCommand.kt b/src/main/kotlin/io/github/mojira/arisa/modules/commands/RemoveContentCommand.kt index 165d2105..74a75246 100644 --- a/src/main/kotlin/io/github/mojira/arisa/modules/commands/RemoveContentCommand.kt +++ b/src/main/kotlin/io/github/mojira/arisa/modules/commands/RemoveContentCommand.kt @@ -76,7 +76,7 @@ class RemoveContentCommand( is Either.Left -> throw CommandExceptions.CANNOT_QUERY_USER_ACTIVITY .create(sanitizedUserName, jql) - .initCause(either.a) + .apply { addSuppressed(either.a) } is Either.Right -> either.b } diff --git a/src/test/kotlin/io/github/mojira/arisa/modules/commands/ListUserActivityCommandTest.kt b/src/test/kotlin/io/github/mojira/arisa/modules/commands/ListUserActivityCommandTest.kt index 7be73b51..c64e964d 100644 --- a/src/test/kotlin/io/github/mojira/arisa/modules/commands/ListUserActivityCommandTest.kt +++ b/src/test/kotlin/io/github/mojira/arisa/modules/commands/ListUserActivityCommandTest.kt @@ -2,9 +2,13 @@ package io.github.mojira.arisa.modules.commands import arrow.core.Either import arrow.core.extensions.fx +import arrow.core.left +import com.mojang.brigadier.exceptions.CommandSyntaxException import io.github.mojira.arisa.utils.mockIssue +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.collections.shouldContainExactly import io.kotest.matchers.nulls.shouldNotBeNull import io.kotest.matchers.shouldBe import io.kotest.matchers.string.shouldContain @@ -72,7 +76,23 @@ class ListUserActivityCommandTest : StringSpec({ comment.shouldNotBeNull() commentRestriction shouldBe "staff" - // Should contain sanitized user name - comment shouldStartWith "No unrestricted comments from user \"user?Name\" were found." + val expectedSanitizedUser = "user?Name" + comment shouldStartWith "No unrestricted comments from user \"$expectedSanitizedUser\" were found." + } + + "should throw with suppressed exception when querying activity fails" { + val searchException = Exception("test exception") + val command = ListUserActivityCommand { _, _ -> + searchException.left() + } + + val issue = mockIssue() + val user = "user\nName" + val exception = shouldThrow { + command(issue, user) + } + val expectedSanitizedUser = "user?Name" + exception.message shouldStartWith "Could not query activity of user \"$expectedSanitizedUser\". Query string: " + exception.suppressed.shouldContainExactly(searchException) } }) diff --git a/src/test/kotlin/io/github/mojira/arisa/modules/commands/RemoveContentCommandTest.kt b/src/test/kotlin/io/github/mojira/arisa/modules/commands/RemoveContentCommandTest.kt index 14fe310c..98d3648f 100644 --- a/src/test/kotlin/io/github/mojira/arisa/modules/commands/RemoveContentCommandTest.kt +++ b/src/test/kotlin/io/github/mojira/arisa/modules/commands/RemoveContentCommandTest.kt @@ -2,13 +2,17 @@ package io.github.mojira.arisa.modules.commands import arrow.core.left import arrow.core.right +import com.mojang.brigadier.exceptions.CommandSyntaxException import io.github.mojira.arisa.utils.mockIssue +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.StringSpec import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.collections.shouldContainExactly import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder import io.kotest.matchers.nulls.shouldNotBeNull import io.kotest.matchers.shouldBe import io.kotest.matchers.string.shouldContain +import io.kotest.matchers.string.shouldStartWith import io.mockk.mockk import net.rcarz.jiraclient.Issue @@ -178,4 +182,22 @@ class RemoveContentCommandTest : StringSpec({ // Should contain sanitized user name comment shouldContain "evil?User" } + + "should throw with suppressed exception when querying activity fails" { + val searchException = Exception("test exception") + val command = RemoveContentCommand( + searchIssues = { _, _ -> searchException.left() }, + getIssue = { throw AssertionError("not needed for test") }, + execute = { throw AssertionError("not needed for test") } + ) + + val issue = mockIssue() + val user = "user\nName" + val exception = shouldThrow { + command(issue, user) + } + val expectedSanitizedUser = "user?Name" + exception.message shouldStartWith "Could not query activity of user \"$expectedSanitizedUser\". Query string: " + exception.suppressed.shouldContainExactly(searchException) + } })