Skip to content

Commit

Permalink
Unify SynqOrderService and SynqProductService
Browse files Browse the repository at this point in the history
  • Loading branch information
anotheroneofthese committed Aug 15, 2024
1 parent 070fcaa commit f5c8a5e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
26 changes: 25 additions & 1 deletion src/main/kotlin/no/nb/mlt/wls/core/data/synq/SynqError.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
package no.nb.mlt.wls.core.data.synq

data class SynqError(val errorCode: Int, val errorText: String)
import org.springframework.web.reactive.function.client.WebClientResponseException
import org.springframework.web.server.ServerErrorException

data class SynqError(val errorCode: Int, val errorText: String) {
companion object {
/**
* Converts a WebClient error into a ServerErrorException.
* This is used for propagating error data to the client.
* @see ServerErrorException
* @see no.nb.mlt.wls.order.service.SynqOrderService
* @see no.nb.mlt.wls.product.service.SynqProductService
*/
fun createServerError(error: WebClientResponseException): ServerErrorException {
val errorBody = error.getResponseBodyAs(SynqError::class.java)

return ServerErrorException(
"Failed to create product in SynQ, the storage system responded with error code: " +
"'${errorBody?.errorCode ?: "NO ERROR CODE FOUND"}' " +
"and error text: " +
"'${errorBody?.errorText ?: "NO ERROR TEXT FOUND"}'",
error
)
}
}
}
40 changes: 14 additions & 26 deletions src/main/kotlin/no/nb/mlt/wls/order/service/SynqOrderService.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package no.nb.mlt.wls.order.service

import kotlinx.coroutines.reactor.awaitSingle
import no.nb.mlt.wls.core.data.synq.SynqError
import no.nb.mlt.wls.core.data.synq.SynqError.Companion.createServerError
import no.nb.mlt.wls.order.payloads.SynqOrder
import no.nb.mlt.wls.order.payloads.SynqOrderPayload
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
import org.springframework.web.client.HttpClientErrorException
import org.springframework.web.reactive.function.BodyInserters
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.server.ServerErrorException
import org.springframework.web.reactive.function.client.WebClientResponseException
import java.net.URI

@Service
Expand All @@ -21,33 +21,21 @@ class SynqOrderService(
@Value("\${synq.path.base}")
lateinit var baseUrl: String

fun createOrder(payload: SynqOrderPayload): ResponseEntity<SynqError> {
suspend fun createOrder(payload: SynqOrderPayload): ResponseEntity<SynqError> {
val uri = URI.create("$baseUrl/orders/batch")

// Wrap the order in the way SynQ likes it
val orders = SynqOrder(listOf(payload))

try {
return ResponseEntity(
webClient
.post()
.uri(uri)
.body(BodyInserters.fromValue(orders))
.retrieve()
.bodyToMono(SynqError::class.java)
.block(),
HttpStatus.CREATED
)
} catch (exception: HttpClientErrorException) {
val errorBody = exception.getResponseBodyAs(SynqError::class.java)

throw ServerErrorException(
"Failed to create product in SynQ, the storage system responded with error code: " +
"'${errorBody?.errorCode ?: "NO ERROR CODE FOUND"}' " +
"and error text: " +
"'${errorBody?.errorText ?: "NO ERROR TEXT FOUND"}'",
exception
)
}
return webClient
.post()
.uri(uri)
.body(BodyInserters.fromValue(orders))
.retrieve()
.toEntity(SynqError::class.java)
.onErrorMap(WebClientResponseException::class.java) {
createServerError(it)
}
.awaitSingle()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nb.mlt.wls.product.service

import kotlinx.coroutines.reactor.awaitSingle
import no.nb.mlt.wls.core.data.synq.SynqError
import no.nb.mlt.wls.core.data.synq.SynqError.Companion.createServerError
import no.nb.mlt.wls.product.payloads.SynqProductPayload
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
Expand Down Expand Up @@ -38,22 +39,10 @@ class SynqProductService(
Mono.error(error)
}
}
.onErrorMap(WebClientResponseException::class.java) { transformSynqError(it) }
.onErrorMap(WebClientResponseException::class.java) { createServerError(it) }
.onErrorReturn(DuplicateProductException::class.java, ResponseEntity.ok().build())
.awaitSingle()
}

fun transformSynqError(error: WebClientResponseException): ServerErrorException {
val errorBody = error.getResponseBodyAs(SynqError::class.java)

return ServerErrorException(
"Failed to create product in SynQ, the storage system responded with error code: " +
"'${errorBody?.errorCode ?: "NO ERROR CODE FOUND"}' " +
"and error text: " +
"'${errorBody?.errorText ?: "NO ERROR TEXT FOUND"}'",
error
)
}
}

class DuplicateProductException(override val cause: Throwable) : ServerErrorException("Product already exists in SynQ", cause)

0 comments on commit f5c8a5e

Please sign in to comment.