Skip to content

Commit 4c73747

Browse files
committed
Polishing
Closes gh-32931
1 parent 611367e commit 4c73747

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

spring-web/src/main/kotlin/org/springframework/web/server/CoWebExceptionHandler.kt

+24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package org.springframework.web.server
218

319
import kotlinx.coroutines.Dispatchers
420
import kotlinx.coroutines.reactor.mono
521
import reactor.core.publisher.Mono
622
import kotlin.coroutines.CoroutineContext
723

24+
/**
25+
* Kotlin-specific implementation of the [WebExceptionHandler] interface that allows for
26+
* using coroutines, including [kotlin.coroutines.CoroutineContext] propagation.
27+
*
28+
* @author Sangyoon Jeong
29+
* @since 6.2
30+
*/
831
abstract class CoWebExceptionHandler : WebExceptionHandler {
32+
933
final override fun handle(exchange: ServerWebExchange, ex: Throwable): Mono<Void> {
1034
val context = exchange.attributes[CoWebFilter.COROUTINE_CONTEXT_ATTRIBUTE] as CoroutineContext?
1135
return mono(context ?: Dispatchers.Unconfined) { coHandle(exchange, ex) }.then()

spring-web/src/test/kotlin/org/springframework/web/server/CoWebExceptionHandlerTests.kt

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package org.springframework.web.server
218

319
import org.assertj.core.api.Assertions.assertThat
@@ -7,6 +23,7 @@ import org.springframework.web.testfixture.server.MockServerWebExchange
723
import reactor.test.StepVerifier
824

925
class CoWebExceptionHandlerTest {
26+
1027
@Test
1128
fun handle() {
1229
val exchange = MockServerWebExchange.from(MockServerHttpRequest.get("https://example.com"))
@@ -22,6 +39,7 @@ class CoWebExceptionHandlerTest {
2239
}
2340

2441
private class MyCoWebExceptionHandler : CoWebExceptionHandler() {
42+
2543
override suspend fun coHandle(exchange: ServerWebExchange, ex: Throwable) {
2644
exchange.attributes["foo"] = "bar"
2745
}

0 commit comments

Comments
 (0)