Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,15 @@ private class KotlinMethodArgumentParser<C, T>(

@Suppress("UNCHECKED_CAST")
private fun <T> CompletableFuture<*>.mapResult(): CompletableFuture<ArgumentParseResult<T>> =
thenApply {
when (it) {
null -> ArgumentParseResult.failure(IllegalArgumentException("Result not found"))
is ArgumentParseResult<*> -> it as ArgumentParseResult<T>
else -> ArgumentParseResult.success((it as T)!!)
handle { result, exception ->
if (exception != null) {
ArgumentParseResult.failure(exception)
} else {
when (result) {
null -> ArgumentParseResult.failure(IllegalArgumentException("Result not found"))
is ArgumentParseResult<*> -> result as ArgumentParseResult<T>
else -> ArgumentParseResult.success((result as T)!!)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,33 @@ class KotlinAnnotatedMethodsTest {
}
}

@Test
fun `test suspending parser method with exception`(): Unit = runBlocking {
AnnotationParser(commandManager, TestCommandSender::class.java)
.installCoroutineSupport()
.parse(ParserMethods())

val commandContext = StandardCommandContextFactory(commandManager).create(
true,
TestCommandSender()
)

val parser = commandManager.parserRegistry().createParser(
TypeToken.get(ParserResult3::class.java),
ParserParameters.empty()
)

assert(parser.isPresent) {
"Suspending parser cannot be found!"
}

val result: ArgumentParseResult<*> = parser.get().parseFuture(commandContext, CommandInput.of("5")).await()

assert(result.failure().orElse(null) is IllegalStateException) {
"Suspending parser should fail with IllegalStateException!"
}
}

public class TestCommandSender

private class TestCommandManager : CommandManager<TestCommandSender>(
Expand Down Expand Up @@ -236,6 +263,8 @@ class KotlinAnnotatedMethodsTest {

data class ParserResult2(val test: Int)

data class ParserResult3(val test: Int)

class ParserMethods {

@Parser
Expand All @@ -249,5 +278,11 @@ class KotlinAnnotatedMethodsTest {
withContext(Dispatchers.Default) {
ArgumentParseResult.success(ParserResult2(input.lastRemainingToken().toInt()))
}

@Parser
suspend fun exceptionParser(input: CommandInput): ParserResult3 =
withContext(Dispatchers.Default) {
throw IllegalStateException()
}
}
}
Loading