#103 - Coroutines extensions throw proper EmptyResultDataAccessException.
RowsFetchSpec.awaitOne() and RowsFetchSpec.awaitFirst() now throw EmptyResultDataAccessException instead of NoSuchElementException to consistently use Spring DAO exceptions.
This commit is contained in:
@@ -18,16 +18,17 @@ package org.springframework.data.r2dbc.core
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.reactive.awaitFirstOrNull
|
||||
import kotlinx.coroutines.reactive.awaitSingle
|
||||
import kotlinx.coroutines.reactive.flow.asFlow
|
||||
import org.springframework.dao.EmptyResultDataAccessException
|
||||
|
||||
/**
|
||||
* Non-nullable Coroutines variant of [RowsFetchSpec.one].
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T =
|
||||
one().awaitSingle()
|
||||
suspend fun <T> RowsFetchSpec<T>.awaitOne(): T {
|
||||
return one().awaitFirstOrNull() ?: throw EmptyResultDataAccessException(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Nullable Coroutines variant of [RowsFetchSpec.one].
|
||||
@@ -42,8 +43,9 @@ suspend fun <T> RowsFetchSpec<T>.awaitOneOrNull(): T? =
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T =
|
||||
first().awaitSingle()
|
||||
suspend fun <T> RowsFetchSpec<T>.awaitFirst(): T {
|
||||
return first().awaitFirstOrNull() ?: throw EmptyResultDataAccessException(1)
|
||||
}
|
||||
|
||||
/**
|
||||
* Nullable Coroutines variant of [RowsFetchSpec.first].
|
||||
@@ -62,4 +64,4 @@ suspend fun <T> RowsFetchSpec<T>.awaitFirstOrNull(): T? =
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@FlowPreview
|
||||
fun <T: Any> RowsFetchSpec<T>.flow(batchSize: Int = 1): Flow<T> = all().asFlow(batchSize)
|
||||
fun <T : Any> RowsFetchSpec<T>.flow(batchSize: Int = 1): Flow<T> = all().asFlow(batchSize)
|
||||
|
||||
@@ -24,6 +24,7 @@ import kotlinx.coroutines.runBlocking
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatExceptionOfType
|
||||
import org.junit.Test
|
||||
import org.springframework.dao.EmptyResultDataAccessException
|
||||
import reactor.core.publisher.Flux
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@@ -31,6 +32,7 @@ import reactor.core.publisher.Mono
|
||||
* Unit tests for [RowsFetchSpec] extensions.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class RowsFetchSpecExtensionsTests {
|
||||
|
||||
@@ -49,13 +51,13 @@ class RowsFetchSpecExtensionsTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test // gh-63
|
||||
@Test // gh-63, gh-103
|
||||
fun awaitOneWithNull() {
|
||||
|
||||
val spec = mockk<RowsFetchSpec<String>>()
|
||||
every { spec.one() } returns Mono.empty()
|
||||
|
||||
assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy {
|
||||
assertThatExceptionOfType(EmptyResultDataAccessException::class.java).isThrownBy {
|
||||
runBlocking { spec.awaitOne() }
|
||||
}
|
||||
|
||||
@@ -109,13 +111,13 @@ class RowsFetchSpecExtensionsTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test // gh-63
|
||||
@Test // gh-63, gh-103
|
||||
fun awaitFirstWithNull() {
|
||||
|
||||
val spec = mockk<RowsFetchSpec<String>>()
|
||||
every { spec.first() } returns Mono.empty()
|
||||
|
||||
assertThatExceptionOfType(NoSuchElementException::class.java).isThrownBy {
|
||||
assertThatExceptionOfType(EmptyResultDataAccessException::class.java).isThrownBy {
|
||||
runBlocking { spec.awaitFirst() }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user