Skip to content

Commit 7403651

Browse files
committed
#103 - Coroutines extensions throw proper EmptyResultDataAccessException.
RowsFetchSpec.awaitOne() and RowsFetchSpec.awaitFirst() now throw EmptyResultDataAccessException instead of NoSuchElementException to consistently use Spring DAO exceptions.
1 parent e15bd31 commit 7403651

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/main/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensions.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ package org.springframework.data.r2dbc.core
1818
import kotlinx.coroutines.FlowPreview
1919
import kotlinx.coroutines.flow.Flow
2020
import kotlinx.coroutines.reactive.awaitFirstOrNull
21-
import kotlinx.coroutines.reactive.awaitSingle
2221
import kotlinx.coroutines.reactive.flow.asFlow
22+
import org.springframework.dao.EmptyResultDataAccessException
2323

2424
/**
2525
* Non-nullable Coroutines variant of [RowsFetchSpec.one].
2626
*
2727
* @author Sebastien Deleuze
2828
*/
29-
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T =
30-
one().awaitSingle()
29+
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T {
30+
return one().awaitFirstOrNull() ?: throw EmptyResultDataAccessException(1)
31+
}
3132

3233
/**
3334
* Nullable Coroutines variant of [RowsFetchSpec.one].
@@ -42,8 +43,9 @@ suspend fun <T> RowsFetchSpec<T>.awaitOneOrNull(): T? =
4243
*
4344
* @author Sebastien Deleuze
4445
*/
45-
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T =
46-
first().awaitSingle()
46+
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T {
47+
return first().awaitFirstOrNull() ?: throw EmptyResultDataAccessException(1)
48+
}
4749

4850
/**
4951
* Nullable Coroutines variant of [RowsFetchSpec.first].
@@ -62,4 +64,4 @@ suspend fun <T> RowsFetchSpec<T>.awaitFirstOrNull(): T? =
6264
* @author Sebastien Deleuze
6365
*/
6466
@FlowPreview
65-
fun <T: Any> RowsFetchSpec<T>.flow(batchSize: Int = 1): Flow<T> = all().asFlow(batchSize)
67+
fun <T : Any> RowsFetchSpec<T>.flow(batchSize: Int = 1): Flow<T> = all().asFlow(batchSize)

src/test/kotlin/org/springframework/data/r2dbc/core/RowsFetchSpecExtensionsTests.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ import kotlinx.coroutines.runBlocking
2424
import org.assertj.core.api.Assertions.assertThat
2525
import org.assertj.core.api.Assertions.assertThatExceptionOfType
2626
import org.junit.Test
27+
import org.springframework.dao.EmptyResultDataAccessException
2728
import reactor.core.publisher.Flux
2829
import reactor.core.publisher.Mono
2930

3031
/**
3132
* Unit tests for [RowsFetchSpec] extensions.
3233
*
3334
* @author Sebastien Deleuze
35+
* @author Mark Paluch
3436
*/
3537
class RowsFetchSpecExtensionsTests {
3638

@@ -49,13 +51,13 @@ class RowsFetchSpecExtensionsTests {
4951
}
5052
}
5153

52-
@Test // gh-63
54+
@Test // gh-63, gh-103
5355
fun awaitOneWithNull() {
5456

5557
val spec = mockk<RowsFetchSpec<String>>()
5658
every { spec.one() } returns Mono.empty()
5759

58-
assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy {
60+
assertThatExceptionOfType(EmptyResultDataAccessException::class.java).isThrownBy {
5961
runBlocking { spec.awaitOne() }
6062
}
6163

@@ -109,13 +111,13 @@ class RowsFetchSpecExtensionsTests {
109111
}
110112
}
111113

112-
@Test // gh-63
114+
@Test // gh-63, gh-103
113115
fun awaitFirstWithNull() {
114116

115117
val spec = mockk<RowsFetchSpec<String>>()
116118
every { spec.first() } returns Mono.empty()
117119

118-
assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy {
120+
assertThatExceptionOfType(EmptyResultDataAccessException::class.java).isThrownBy {
119121
runBlocking { spec.awaitFirst() }
120122
}
121123

0 commit comments

Comments
 (0)