Skip to content

Commit

Permalink
Improve error message when creation of a new user fails
Browse files Browse the repository at this point in the history
  • Loading branch information
barreiro authored and johnaohara committed Jan 12, 2024
1 parent 69717ec commit e92bcc7
Showing 1 changed file with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,13 @@ public void createUser(NewUser user) {
Response response = keycloak.realm(realm).users().create(rep);
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
log.errorf("Failed to create new user %s: %s", rep, response);
throw ServiceException.badRequest("Failed to create new user.");
if (!keycloak.realm(realm).users().search(rep.getUsername(), true).isEmpty()) {
throw ServiceException.badRequest("User exists with same username.");
} else if (!keycloak.realm(realm).users().searchByEmail(rep.getEmail(), true).isEmpty()) {
throw ServiceException.badRequest("User exists with same email.");
} else {
throw ServiceException.badRequest("Failed to create new user: " + response.getStatusInfo().getReasonPhrase());
}
}
List<UserRepresentation> matchingUsers = keycloak.realm(realm).users().search(rep.getUsername(), true);
if (matchingUsers == null || matchingUsers.isEmpty()) {
Expand Down

0 comments on commit e92bcc7

Please sign in to comment.