clean up error handling
extract remote service error handling - use helper in AccountRegistryService - use helper in DeviceRegistryService - add responseErrorHandler in Helper.kt - add hlaejaErrorHandler in Helper.kt change NoChangeException to extend HlaejaException change NotFoundException to extend HlaejaException
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
package ltd.hlaeja.exception
|
package ltd.hlaeja.exception
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
open class NoChangeException : AccountRegistryException {
|
open class NoChangeException : HlaejaException {
|
||||||
|
|
||||||
constructor() : super()
|
constructor() : super()
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package ltd.hlaeja.exception
|
package ltd.hlaeja.exception
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
open class NotFoundException : AccountRegistryException {
|
open class NotFoundException : HlaejaException {
|
||||||
|
|
||||||
constructor() : super()
|
constructor() : super()
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package ltd.hlaeja.service
|
|||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import ltd.hlaeja.exception.AccountRegistryException
|
|
||||||
import ltd.hlaeja.library.accountRegistry.Account
|
import ltd.hlaeja.library.accountRegistry.Account
|
||||||
import ltd.hlaeja.library.accountRegistry.Authentication
|
import ltd.hlaeja.library.accountRegistry.Authentication
|
||||||
import ltd.hlaeja.property.AccountRegistryProperty
|
import ltd.hlaeja.property.AccountRegistryProperty
|
||||||
@@ -11,6 +10,8 @@ import ltd.hlaeja.util.accountRegistryAccounts
|
|||||||
import ltd.hlaeja.util.accountRegistryAuthenticate
|
import ltd.hlaeja.util.accountRegistryAuthenticate
|
||||||
import ltd.hlaeja.util.accountRegistryCreate
|
import ltd.hlaeja.util.accountRegistryCreate
|
||||||
import ltd.hlaeja.util.accountRegistryUpdate
|
import ltd.hlaeja.util.accountRegistryUpdate
|
||||||
|
import ltd.hlaeja.util.hlaejaErrorHandler
|
||||||
|
import ltd.hlaeja.util.responseErrorHandler
|
||||||
import org.springframework.http.HttpStatus.BAD_REQUEST
|
import org.springframework.http.HttpStatus.BAD_REQUEST
|
||||||
import org.springframework.security.authentication.AuthenticationServiceException
|
import org.springframework.security.authentication.AuthenticationServiceException
|
||||||
import org.springframework.security.core.AuthenticationException
|
import org.springframework.security.core.AuthenticationException
|
||||||
@@ -63,33 +64,18 @@ class AccountRegistryService(
|
|||||||
fun addAccount(
|
fun addAccount(
|
||||||
request: Account.Request,
|
request: Account.Request,
|
||||||
): Mono<Account.Response> = webClient.accountRegistryCreate(request, property)
|
): Mono<Account.Response> = webClient.accountRegistryCreate(request, property)
|
||||||
.onErrorResume { error ->
|
.onErrorResume(::hlaejaErrorHandler)
|
||||||
when (error) {
|
|
||||||
is AccountRegistryException -> Mono.error(error)
|
|
||||||
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getAccount(
|
fun getAccount(
|
||||||
account: UUID,
|
account: UUID,
|
||||||
): Mono<Account.Response> = webClient.accountRegistryAccount(account, property)
|
): Mono<Account.Response> = webClient.accountRegistryAccount(account, property)
|
||||||
.onErrorResume { error ->
|
.onErrorResume(::responseErrorHandler)
|
||||||
when (error) {
|
|
||||||
is ResponseStatusException -> Mono.error(error)
|
|
||||||
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateAccount(
|
fun updateAccount(
|
||||||
account: UUID,
|
account: UUID,
|
||||||
request: Account.Request,
|
request: Account.Request,
|
||||||
): Mono<Account.Response> = webClient.accountRegistryUpdate(account, request, property)
|
): Mono<Account.Response> = webClient.accountRegistryUpdate(account, request, property)
|
||||||
.onErrorResume { error ->
|
.onErrorResume(::hlaejaErrorHandler)
|
||||||
when (error) {
|
|
||||||
is AccountRegistryException -> Mono.error(error)
|
|
||||||
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO implement user gropes and access
|
// TODO implement user gropes and access
|
||||||
fun getRoles(): Map<String, List<String>> = mapOf(
|
fun getRoles(): Map<String, List<String>> = mapOf(
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package ltd.hlaeja.service
|
package ltd.hlaeja.service
|
||||||
|
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import ltd.hlaeja.exception.DeviceRegistryException
|
|
||||||
import ltd.hlaeja.exception.HlaejaException
|
|
||||||
import ltd.hlaeja.library.deviceRegistry.Type
|
import ltd.hlaeja.library.deviceRegistry.Type
|
||||||
import ltd.hlaeja.library.deviceRegistry.Types
|
import ltd.hlaeja.library.deviceRegistry.Types
|
||||||
import ltd.hlaeja.property.DeviceRegistryProperty
|
import ltd.hlaeja.property.DeviceRegistryProperty
|
||||||
@@ -10,10 +8,10 @@ import ltd.hlaeja.util.deviceRegistryType
|
|||||||
import ltd.hlaeja.util.deviceRegistryTypes
|
import ltd.hlaeja.util.deviceRegistryTypes
|
||||||
import ltd.hlaeja.util.deviceRegistryTypesCreate
|
import ltd.hlaeja.util.deviceRegistryTypesCreate
|
||||||
import ltd.hlaeja.util.deviceRegistryTypesUpdate
|
import ltd.hlaeja.util.deviceRegistryTypesUpdate
|
||||||
import org.springframework.http.HttpStatus.BAD_REQUEST
|
import ltd.hlaeja.util.hlaejaErrorHandler
|
||||||
|
import ltd.hlaeja.util.responseErrorHandler
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
import org.springframework.web.reactive.function.client.WebClient
|
import org.springframework.web.reactive.function.client.WebClient
|
||||||
import org.springframework.web.server.ResponseStatusException
|
|
||||||
import reactor.core.publisher.Flux
|
import reactor.core.publisher.Flux
|
||||||
import reactor.core.publisher.Mono
|
import reactor.core.publisher.Mono
|
||||||
|
|
||||||
@@ -31,33 +29,16 @@ class DeviceRegistryService(
|
|||||||
fun createType(
|
fun createType(
|
||||||
request: Type.Request,
|
request: Type.Request,
|
||||||
): Mono<Type.Response> = webClient.deviceRegistryTypesCreate(request, property)
|
): Mono<Type.Response> = webClient.deviceRegistryTypesCreate(request, property)
|
||||||
.onErrorResume { error ->
|
.onErrorResume(::hlaejaErrorHandler)
|
||||||
when (error) {
|
|
||||||
is DeviceRegistryException -> Mono.error(error)
|
|
||||||
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getType(
|
fun getType(
|
||||||
type: UUID,
|
type: UUID,
|
||||||
): Mono<Type.Response> = webClient.deviceRegistryType(type, property)
|
): Mono<Type.Response> = webClient.deviceRegistryType(type, property)
|
||||||
.onErrorResume { error ->
|
.onErrorResume(::responseErrorHandler)
|
||||||
when (error) {
|
|
||||||
is ResponseStatusException -> Mono.error(error)
|
|
||||||
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun updateType(
|
fun updateType(
|
||||||
type: UUID,
|
type: UUID,
|
||||||
request: Type.Request,
|
request: Type.Request,
|
||||||
): Mono<Type.Response> = webClient.deviceRegistryTypesUpdate(type, request, property)
|
): Mono<Type.Response> = webClient.deviceRegistryTypesUpdate(type, request, property)
|
||||||
.onErrorResume(::errorHandler)
|
.onErrorResume(::hlaejaErrorHandler)
|
||||||
|
|
||||||
private fun errorHandler(
|
|
||||||
error: Throwable,
|
|
||||||
): Mono<out Type.Response> = when (error) {
|
|
||||||
is HlaejaException -> Mono.error(error)
|
|
||||||
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,25 @@
|
|||||||
package ltd.hlaeja.util
|
package ltd.hlaeja.util
|
||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
|
import ltd.hlaeja.exception.HlaejaException
|
||||||
|
import org.springframework.http.HttpStatus.BAD_REQUEST
|
||||||
|
import org.springframework.web.server.ResponseStatusException
|
||||||
|
import reactor.core.publisher.Mono
|
||||||
|
|
||||||
private val log = KotlinLogging.logger {}
|
private val log = KotlinLogging.logger {}
|
||||||
|
|
||||||
fun logCall(url: String) = log.debug { "calling: $url" }
|
fun logCall(url: String) = log.debug { "calling: $url" }
|
||||||
|
|
||||||
|
fun <R> hlaejaErrorHandler(
|
||||||
|
error: Throwable,
|
||||||
|
): Mono<out R> = when (error) {
|
||||||
|
is HlaejaException -> Mono.error(error)
|
||||||
|
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <R> responseErrorHandler(
|
||||||
|
error: Throwable,
|
||||||
|
): Mono<out R> = when (error) {
|
||||||
|
is ResponseStatusException -> Mono.error(error)
|
||||||
|
else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user