I am invoking an API which returns the following output while invoking via a tool like Postman.
Error Code: 409 CONFLICT
Error Payload
<code>{
"error": {
"code": 20054,
"message": "The display name 'pr-test' is already used by another subaccount or directory under the parent of this entity. Display names of account entities under the same parent must be unique.",
"target": "/accounts/v1/subaccounts",
"correlationID": "7c8e0376-6d36-4038-45ea-9de453f732ef"
}
}
</code>
<code>{
"error": {
"code": 20054,
"message": "The display name 'pr-test' is already used by another subaccount or directory under the parent of this entity. Display names of account entities under the same parent must be unique.",
"target": "/accounts/v1/subaccounts",
"correlationID": "7c8e0376-6d36-4038-45ea-9de453f732ef"
}
}
</code>
{
"error": {
"code": 20054,
"message": "The display name 'pr-test' is already used by another subaccount or directory under the parent of this entity. Display names of account entities under the same parent must be unique.",
"target": "/accounts/v1/subaccounts",
"correlationID": "7c8e0376-6d36-4038-45ea-9de453f732ef"
}
}
What I am looking at is to access this payload, extract the error.message and throw a custom error.
My Webclient code looks like this:
<code> webClient.post()
.uri(accountsSvcUrl)
.bodyValue(createConsumerSubAccountPayload())
.header(HttpHeaders.AUTHORIZATION, "Bearer "+ credentialManager.getOauthToken(credentialManager.CLOUD_MANAGEMENT_CENTRAL))
.retrieve()
.onStatus(HttpStatusCode::isError,
error -> Mono.error(new RuntimeException
("Error while invoking the API Code: "+error.statusCode()+ " ")))
.bodyToMono(SubAccountResponse.class)
.block();
</code>
<code> webClient.post()
.uri(accountsSvcUrl)
.bodyValue(createConsumerSubAccountPayload())
.header(HttpHeaders.AUTHORIZATION, "Bearer "+ credentialManager.getOauthToken(credentialManager.CLOUD_MANAGEMENT_CENTRAL))
.retrieve()
.onStatus(HttpStatusCode::isError,
error -> Mono.error(new RuntimeException
("Error while invoking the API Code: "+error.statusCode()+ " ")))
.bodyToMono(SubAccountResponse.class)
.block();
</code>
webClient.post()
.uri(accountsSvcUrl)
.bodyValue(createConsumerSubAccountPayload())
.header(HttpHeaders.AUTHORIZATION, "Bearer "+ credentialManager.getOauthToken(credentialManager.CLOUD_MANAGEMENT_CENTRAL))
.retrieve()
.onStatus(HttpStatusCode::isError,
error -> Mono.error(new RuntimeException
("Error while invoking the API Code: "+error.statusCode()+ " ")))
.bodyToMono(SubAccountResponse.class)
.block();
I want some help in understanding how can I get the error body under the onStatus block.
Sincerely appreciate your advice.
Prabal