File tree 2 files changed +43
-0
lines changed
main/kotlin/org/springframework/web/server
test/kotlin/org/springframework/web/server
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ package org.springframework.web.server
2
+
3
+ import kotlinx.coroutines.Dispatchers
4
+ import kotlinx.coroutines.reactor.mono
5
+ import reactor.core.publisher.Mono
6
+ import kotlin.coroutines.CoroutineContext
7
+
8
+ abstract class CoWebExceptionHandler : WebExceptionHandler {
9
+ final override fun handle (exchange : ServerWebExchange , ex : Throwable ): Mono <Void > {
10
+ val context = exchange.attributes[CoWebFilter .COROUTINE_CONTEXT_ATTRIBUTE ] as CoroutineContext ?
11
+ return mono(context ? : Dispatchers .Unconfined ) { coHandle(exchange, ex) }.then()
12
+ }
13
+
14
+ protected abstract suspend fun coHandle (exchange : ServerWebExchange , ex : Throwable )
15
+ }
Original file line number Diff line number Diff line change
1
+ package org.springframework.web.server
2
+
3
+ import org.assertj.core.api.Assertions.assertThat
4
+ import org.junit.jupiter.api.Test
5
+ import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest
6
+ import org.springframework.web.testfixture.server.MockServerWebExchange
7
+ import reactor.test.StepVerifier
8
+
9
+ class CoWebExceptionHandlerTest {
10
+ @Test
11
+ fun handle () {
12
+ val exchange = MockServerWebExchange .from(MockServerHttpRequest .get(" https://example.com" ))
13
+ val ex = RuntimeException ()
14
+
15
+ val handler = MyCoWebExceptionHandler ()
16
+ val result = handler.handle(exchange, ex)
17
+
18
+ StepVerifier .create(result).verifyComplete()
19
+
20
+ assertThat(exchange.attributes[" foo" ]).isEqualTo(" bar" )
21
+ }
22
+ }
23
+
24
+ private class MyCoWebExceptionHandler : CoWebExceptionHandler () {
25
+ override suspend fun coHandle (exchange : ServerWebExchange , ex : Throwable ) {
26
+ exchange.attributes[" foo" ] = " bar"
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments