Skip to content

Commit

Permalink
#103 - Coroutines extensions throw proper EmptyResultDataAccessExcept…
Browse files Browse the repository at this point in the history
…ion.

RowsFetchSpec.awaitOne() and RowsFetchSpec.awaitFirst() now throw EmptyResultDataAccessException instead of NoSuchElementException to consistently use Spring DAO exceptions.
  • Loading branch information
mp911de committed Jul 15, 2019
1 parent e15bd31 commit 7403651
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -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].
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ 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

/**
* Unit tests for [RowsFetchSpec] extensions.
*
* @author Sebastien Deleuze
* @author Mark Paluch
*/
class RowsFetchSpecExtensionsTests {

Expand All @@ -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() }
}

Expand Down Expand Up @@ -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() }
}

Expand Down

0 comments on commit 7403651

Please sign in to comment.