Skip to content

Commit

Permalink
fix(rest.identity): adding specific error messages when user not found
Browse files Browse the repository at this point in the history
Signed-off-by: SimoneFiorani <[email protected]>
  • Loading branch information
sfiorani committed Dec 6, 2023
1 parent 5aada0c commit e4558fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import java.util.Optional;
import java.util.Set;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response.Status;

import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.configuration.ComponentConfiguration;
import org.eclipse.kura.configuration.ConfigurationService;
import org.eclipse.kura.crypto.CryptoService;
import org.eclipse.kura.internal.rest.identity.provider.dto.UserDTO;
import org.eclipse.kura.request.handler.jaxrs.DefaultExceptionHandler;
import org.eclipse.kura.util.useradmin.UserAdminHelper;
import org.eclipse.kura.util.useradmin.UserAdminHelper.FallibleConsumer;
import org.eclipse.kura.util.validation.PasswordStrengthValidators;
Expand Down Expand Up @@ -77,18 +81,24 @@ public void createUser(UserDTO user) throws KuraException {
}
}

public void deleteUser(String userName) {
this.userAdminHelper.deleteUser(userName);
public void deleteUser(String userName) throws WebApplicationException {

if (this.userAdminHelper.getUser(userName).isPresent()) {
this.userAdminHelper.deleteUser(userName);
} else {
throw DefaultExceptionHandler.buildWebApplicationException(Status.NOT_FOUND, "Identity does not exist");
}

}

public UserDTO getUser(String userName) throws KuraException {
public UserDTO getUser(String userName) throws WebApplicationException {
Optional<User> user = this.userAdminHelper.getUser(userName);
if (user.isPresent()) {
UserDTO userFound = initUserConfig(user.get());
fillPermissions(Collections.singletonMap(user.get().getName(), userFound));
return userFound;
} else {
throw new KuraException(KuraErrorCode.NOT_FOUND, IDENTITY + userName + " not found");
throw DefaultExceptionHandler.buildWebApplicationException(Status.NOT_FOUND, "Identity does not exist");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.Objects;
import java.util.Set;

import javax.ws.rs.WebApplicationException;

import org.eclipse.kura.KuraException;
import org.eclipse.kura.configuration.ComponentConfiguration;
import org.eclipse.kura.configuration.ConfigurationService;
Expand Down Expand Up @@ -119,7 +121,7 @@ public void shoulGetUser() {
private void whenGettingUser(String username) {
try {
this.user = this.identityService.getUser(username);
} catch (KuraException e) {
} catch (WebApplicationException e) {
this.occurredException = e;
}
}
Expand Down

0 comments on commit e4558fc

Please sign in to comment.