add account not found exception

- update balance in AccountController with onErrorResume
- update AccountService
  - update getForUpdateById with switchIfEmpty
  - update getById with switchIfEmpty
- add AccountNotFoundException
This commit is contained in:
2025-09-11 18:49:16 +02:00
parent 25f534b6e3
commit e46bf55232
5 changed files with 56 additions and 6 deletions

View File

@@ -39,6 +39,6 @@ class AccountController(
@PathVariable account: UUID,
): Mono<Account.Response> = accountService.getById(account)
.map { it.toResponse() }
.switchIfEmpty(Mono.error(ResponseStatusException(NOT_FOUND)))
.onErrorResume { Mono.error(ResponseStatusException(NOT_FOUND)) }
.doOnError { log.debug { "account $account not found for balance" } }
}

View File

@@ -0,0 +1,10 @@
package ltd.lulz.exception
@Suppress("unused")
class AccountNotFoundException : RuntimeException {
constructor() : super()
constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?) : super(message, cause)
constructor(cause: Throwable?) : super(cause)
}

View File

@@ -2,6 +2,7 @@ package ltd.lulz.service
import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.UUID
import ltd.lulz.exception.AccountNotFoundException
import ltd.lulz.model.AccountEntity
import ltd.lulz.repository.AccountRepository
import org.springframework.stereotype.Service
@@ -15,18 +16,28 @@ class AccountService(
private val accountRepository: AccountRepository,
) {
fun create(entity: AccountEntity): Mono<AccountEntity> = accountRepository
fun create(
entity: AccountEntity,
): Mono<AccountEntity> = accountRepository
.save(entity)
.doOnNext { log.debug { "account created with id: ${it.id}" } }
fun getById(id: UUID): Mono<AccountEntity> = accountRepository.findById(id)
fun getById(
id: UUID,
): Mono<AccountEntity> = accountRepository.findById(id)
.doOnNext { log.debug { "found account by id: ${it.id}" } }
.switchIfEmpty(Mono.error(AccountNotFoundException()))
@Transactional
fun getForUpdateById(id: UUID): Mono<AccountEntity> = accountRepository.findByIdForUpdate(id)
fun getForUpdateById(
id: UUID,
): Mono<AccountEntity> = accountRepository.findByIdForUpdate(id)
.doOnNext { log.trace { "account with id: ${it.id} locked for update" } }
.switchIfEmpty(Mono.error(AccountNotFoundException()))
@Transactional
fun save(entity: AccountEntity): Mono<AccountEntity> = accountRepository.save(entity)
fun save(
entity: AccountEntity,
): Mono<AccountEntity> = accountRepository.save(entity)
.doOnNext { log.trace { "account with id: ${it.id} saved" } }
}