CreatedResponseUtil.getCreatedId should expose server error message (#34343)
We now expose the actual error message found in the response if present. Fixes #34343 Signed-off-by: Thomas Darimont <thomas.darimont@googlemail.com> (cherry picked from commit d94e388047418549d7228280782e804087b2586e)
This commit is contained in:
parent
4640dc19ee
commit
0426e91e7b
1 changed files with 21 additions and 3 deletions
|
|
@ -17,8 +17,11 @@
|
|||
package org.keycloak.admin.client;
|
||||
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A Utility class that parses the Response object into the underlying ID attribute
|
||||
|
|
@ -39,9 +42,24 @@ public class CreatedResponseUtil {
|
|||
URI location = response.getLocation();
|
||||
if (!response.getStatusInfo().equals(Response.Status.CREATED)) {
|
||||
Response.StatusType statusInfo = response.getStatusInfo();
|
||||
throw new WebApplicationException("Create method returned status " +
|
||||
statusInfo.getReasonPhrase() + " (Code: " + statusInfo.getStatusCode() + "); " +
|
||||
"expected status: Created (201)", response);
|
||||
String contentType = response.getHeaderString(HttpHeaders.CONTENT_TYPE);
|
||||
String errorMessage = "Create method returned status " +
|
||||
statusInfo.getReasonPhrase() + " (Code: " + statusInfo.getStatusCode() + "); " +
|
||||
"expected status: Created (201).";
|
||||
if (MediaType.APPLICATION_JSON.equals(contentType)) {
|
||||
// try to add actual server error message to the exception message
|
||||
try {
|
||||
@SuppressWarnings("raw")
|
||||
Map responseBody = response.readEntity(Map.class);
|
||||
if (responseBody != null && responseBody.containsKey("errorMessage")) {
|
||||
errorMessage += " ErrorMessage: " + responseBody.get("errorMessage");
|
||||
}
|
||||
} catch(Exception ignored) {
|
||||
// ignore if we couldn't parse the response
|
||||
}
|
||||
}
|
||||
|
||||
throw new WebApplicationException(errorMessage, response);
|
||||
}
|
||||
if (location == null) {
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue