-
Notifications
You must be signed in to change notification settings - Fork 38k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add coroutine variant of WebExceptionHandler
- Loading branch information
1 parent
bdc4ecd
commit 10e7dc7
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
spring-web/src/main/kotlin/org/springframework/web/server/CoWebExceptionHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.springframework.web.server | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.reactor.mono | ||
import reactor.core.publisher.Mono | ||
|
||
abstract class CoWebExceptionHandler : WebExceptionHandler { | ||
final override fun handle(exchange: ServerWebExchange, ex: Throwable): Mono<Void> = | ||
mono(Dispatchers.Unconfined) { coHandle(exchange, ex) }.then() | ||
|
||
|
||
protected abstract suspend fun coHandle(exchange: ServerWebExchange, ex: Throwable) | ||
} |
28 changes: 28 additions & 0 deletions
28
spring-web/src/test/kotlin/org/springframework/web/server/CoWebExceptionHandlerTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.springframework.web.server | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest | ||
import org.springframework.web.testfixture.server.MockServerWebExchange | ||
import reactor.test.StepVerifier | ||
|
||
class CoWebExceptionHandlerTest { | ||
@Test | ||
fun handle() { | ||
val exchange = MockServerWebExchange.from(MockServerHttpRequest.get("https://example.com")) | ||
val ex = RuntimeException() | ||
|
||
val handler = MyCoWebExceptionHandler() | ||
val result = handler.handle(exchange, ex) | ||
|
||
StepVerifier.create(result).verifyComplete() | ||
|
||
assertThat(exchange.attributes["foo"]).isEqualTo("bar") | ||
} | ||
} | ||
|
||
private class MyCoWebExceptionHandler : CoWebExceptionHandler() { | ||
override suspend fun coHandle(exchange: ServerWebExchange, ex: Throwable) { | ||
exchange.attributes["foo"] = "bar" | ||
} | ||
} |