I have the below api to create a biller
@PostMapping
fun createBiller(@RequestBody @Valid action: CreateBillerRequest, principal: Principal): Mono<ExternalId> {
return commandGateway.send(
RequestCreateBiller(
externalId = ExternalId(),
code = action.code,
description = action.description,
type = action.type,
enabled = action.enabled
)
)
}
a simple aggregate
@Aggregate
internal class BillerAggregate() {
@AggregateIdentifier
private lateinit var externalId: ExternalId
@CommandHandler
@CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING)
fun handle(command: RequestCreateBiller) {
AggregateLifecycle.apply(
CreateBillerRequested(
command.externalId, command.code, command.description, command.type, command.enabled, command.actionBy
)
)
}
@EventSourcingHandler
fun on(event: CreateBillerRequested) {
externalId = event.externalId
}
}
In the below saga i invoke an external system to create the biller. In the error case i throw the exception WebClientFunctionalException
@StartSaga
@SagaEventHandler(associationProperty = "externalId")
fun handle(event: CreateBillerRequested) {
SagaLifecycle.associateWith("externalId", event.externalId.asReference())
webClient.post().uri(applicationProperties.edg.urls.biller)
.body(Mono.just(event), CreateBillerRequested::class.java).retrieve()
.onStatus({ it.isError }) {
val statusCode = it.statusCode()
it.bodyToMono(String::class.java)
.switchIfEmpty(Mono.just(statusCode.value().toString() + " " + statusCode.reasonPhrase))
.flatMap { response ->
val message = WebClientErrorHandler.getOriginalApiErrorMessage(it, response).orElse(response)
return@flatMap Mono.error(WebClientFunctionalException(message))
}
}.bodyToMono(ConfirmBillerResponse::class.java)
.block()
}
The problem is when i test the api in the error case, the api returns always status 200.
So my questions are:
Is the invokation of the external system in this case in the right place?
In case of error, how can i make the api returns the exception WebClientFunctionalException
?