diff --git a/src/main/kotlin/ltd/hlaeja/exception/NoChangeException.kt b/src/main/kotlin/ltd/hlaeja/exception/NoChangeException.kt index 2029703..662af52 100644 --- a/src/main/kotlin/ltd/hlaeja/exception/NoChangeException.kt +++ b/src/main/kotlin/ltd/hlaeja/exception/NoChangeException.kt @@ -1,7 +1,7 @@ package ltd.hlaeja.exception @Suppress("unused") -open class NoChangeException : AccountRegistryException { +open class NoChangeException : HlaejaException { constructor() : super() diff --git a/src/main/kotlin/ltd/hlaeja/exception/NotFoundException.kt b/src/main/kotlin/ltd/hlaeja/exception/NotFoundException.kt index 079adf1..df18b6d 100644 --- a/src/main/kotlin/ltd/hlaeja/exception/NotFoundException.kt +++ b/src/main/kotlin/ltd/hlaeja/exception/NotFoundException.kt @@ -1,7 +1,7 @@ package ltd.hlaeja.exception @Suppress("unused") -open class NotFoundException : AccountRegistryException { +open class NotFoundException : HlaejaException { constructor() : super() diff --git a/src/main/kotlin/ltd/hlaeja/service/AccountRegistryService.kt b/src/main/kotlin/ltd/hlaeja/service/AccountRegistryService.kt index 68bd518..c46fe87 100644 --- a/src/main/kotlin/ltd/hlaeja/service/AccountRegistryService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/AccountRegistryService.kt @@ -2,7 +2,6 @@ package ltd.hlaeja.service import io.github.oshai.kotlinlogging.KotlinLogging import java.util.UUID -import ltd.hlaeja.exception.AccountRegistryException import ltd.hlaeja.library.accountRegistry.Account import ltd.hlaeja.library.accountRegistry.Authentication import ltd.hlaeja.property.AccountRegistryProperty @@ -11,6 +10,8 @@ import ltd.hlaeja.util.accountRegistryAccounts import ltd.hlaeja.util.accountRegistryAuthenticate import ltd.hlaeja.util.accountRegistryCreate 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.security.authentication.AuthenticationServiceException import org.springframework.security.core.AuthenticationException @@ -63,33 +64,18 @@ class AccountRegistryService( fun addAccount( request: Account.Request, ): Mono = webClient.accountRegistryCreate(request, property) - .onErrorResume { error -> - when (error) { - is AccountRegistryException -> Mono.error(error) - else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message)) - } - } + .onErrorResume(::hlaejaErrorHandler) fun getAccount( account: UUID, ): Mono = webClient.accountRegistryAccount(account, property) - .onErrorResume { error -> - when (error) { - is ResponseStatusException -> Mono.error(error) - else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message)) - } - } + .onErrorResume(::responseErrorHandler) fun updateAccount( account: UUID, request: Account.Request, ): Mono = webClient.accountRegistryUpdate(account, request, property) - .onErrorResume { error -> - when (error) { - is AccountRegistryException -> Mono.error(error) - else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message)) - } - } + .onErrorResume(::hlaejaErrorHandler) // TODO implement user gropes and access fun getRoles(): Map> = mapOf( diff --git a/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt b/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt index 0a42dcf..9b53e47 100644 --- a/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/DeviceRegistryService.kt @@ -1,8 +1,6 @@ package ltd.hlaeja.service 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.Types import ltd.hlaeja.property.DeviceRegistryProperty @@ -10,10 +8,10 @@ import ltd.hlaeja.util.deviceRegistryType import ltd.hlaeja.util.deviceRegistryTypes import ltd.hlaeja.util.deviceRegistryTypesCreate 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.web.reactive.function.client.WebClient -import org.springframework.web.server.ResponseStatusException import reactor.core.publisher.Flux import reactor.core.publisher.Mono @@ -31,33 +29,16 @@ class DeviceRegistryService( fun createType( request: Type.Request, ): Mono = webClient.deviceRegistryTypesCreate(request, property) - .onErrorResume { error -> - when (error) { - is DeviceRegistryException -> Mono.error(error) - else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message)) - } - } + .onErrorResume(::hlaejaErrorHandler) fun getType( type: UUID, ): Mono = webClient.deviceRegistryType(type, property) - .onErrorResume { error -> - when (error) { - is ResponseStatusException -> Mono.error(error) - else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message)) - } - } + .onErrorResume(::responseErrorHandler) fun updateType( type: UUID, request: Type.Request, ): Mono = webClient.deviceRegistryTypesUpdate(type, request, property) - .onErrorResume(::errorHandler) - - private fun errorHandler( - error: Throwable, - ): Mono = when (error) { - is HlaejaException -> Mono.error(error) - else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message)) - } + .onErrorResume(::hlaejaErrorHandler) } diff --git a/src/main/kotlin/ltd/hlaeja/util/Helper.kt b/src/main/kotlin/ltd/hlaeja/util/Helper.kt index 64b7734..bab6c1e 100644 --- a/src/main/kotlin/ltd/hlaeja/util/Helper.kt +++ b/src/main/kotlin/ltd/hlaeja/util/Helper.kt @@ -1,7 +1,25 @@ package ltd.hlaeja.util 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 {} fun logCall(url: String) = log.debug { "calling: $url" } + +fun hlaejaErrorHandler( + error: Throwable, +): Mono = when (error) { + is HlaejaException -> Mono.error(error) + else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message)) +} + +fun responseErrorHandler( + error: Throwable, +): Mono = when (error) { + is ResponseStatusException -> Mono.error(error) + else -> Mono.error(ResponseStatusException(BAD_REQUEST, error.message)) +}