fix: adds a general error message and descriptions for some exceptions (#25806)

closes: #25746

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
This commit is contained in:
Steven Hawkins 2024-01-08 13:19:40 -05:00 committed by GitHub
parent a64c00dbef
commit d1d1d69840
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 10 deletions

View file

@ -85,7 +85,8 @@ public class KeycloakErrorHandler implements ExceptionMapper<Throwable> {
OAuth2ErrorRepresentation error = new OAuth2ErrorRepresentation();
error.setError(getErrorCode(throwable));
error.setErrorDescription("For more on this error consult the server log at the debug level.");
return Response.status(statusCode)
.header(HttpHeaders.CONTENT_TYPE, jakarta.ws.rs.core.MediaType.APPLICATION_JSON_TYPE.toString())
.entity(error)
@ -125,11 +126,11 @@ public class KeycloakErrorHandler implements ExceptionMapper<Throwable> {
if (throwable instanceof JsonProcessingException) {
status = Response.Status.BAD_REQUEST.getStatusCode();
}
if (throwable instanceof ModelDuplicateException) {
status = Response.Status.CONFLICT.getStatusCode();
}
return status;
}

View file

@ -197,7 +197,9 @@ public class ClientAttributeCertificateResource {
CertificateRepresentation info = new CertificateRepresentation();
MultivaluedMap<String, FormPartValue> uploadForm = session.getContext().getHttpRequest().getMultiPartFormParameters();
FormPartValue keystoreFormatPart = uploadForm.getFirst("keystoreFormat");
if (keystoreFormatPart == null) throw new BadRequestException();
if (keystoreFormatPart == null) {
throw new BadRequestException("keystoreFormat cannot be null");
}
String keystoreFormat = keystoreFormatPart.asString();
FormPartValue inputParts = uploadForm.getFirst("file");
if (keystoreFormat.equals(CERTIFICATE_PEM)) {

View file

@ -145,7 +145,7 @@ public class ComponentResource {
} catch (ComponentValidationException e) {
return localizedErrorResponse(e);
} catch (IllegalArgumentException e) {
throw new BadRequestException(e);
throw new BadRequestException("Invalid provider type or no such provider", e);
}
}
@ -184,7 +184,7 @@ public class ComponentResource {
} catch (ComponentValidationException e) {
return localizedErrorResponse(e);
} catch (IllegalArgumentException e) {
throw new BadRequestException();
throw new BadRequestException("Invalid provider type or no such provider", e);
}
}
@DELETE

View file

@ -143,7 +143,7 @@ public class RoleContainerResource extends RoleResource {
auth.roles().requireManage(roleContainer);
if (rep.getName() == null) {
throw new BadRequestException();
throw new BadRequestException("role has no name");
}
try {

View file

@ -26,7 +26,6 @@ import org.keycloak.util.JsonSerialization;
import org.keycloak.utils.MediaType;
import org.openqa.selenium.By;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@ -38,10 +37,13 @@ import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import jakarta.ws.rs.core.Response;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.keycloak.utils.MediaType.APPLICATION_JSON;
@ -95,7 +97,7 @@ public class UncaughtErrorPageTest extends AbstractKeycloakTest {
OAuth2ErrorRepresentation error = JsonSerialization.readValue(response.getEntity().getContent(), OAuth2ErrorRepresentation.class);
assertEquals(OAuthErrorException.INVALID_REQUEST, error.getError());
assertNull(error.getErrorDescription());
assertNotNull(error.getErrorDescription());
}
}
@ -115,7 +117,7 @@ public class UncaughtErrorPageTest extends AbstractKeycloakTest {
OAuth2ErrorRepresentation error = JsonSerialization.readValue(response.getEntity().getContent(), OAuth2ErrorRepresentation.class);
assertEquals(OAuthErrorException.INVALID_REQUEST, error.getError());
assertNull(error.getErrorDescription());
assertNotNull(error.getErrorDescription());
}
}