Skip to content

Commit

Permalink
Updated exception throwing strategy
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 e4558fc commit a3d85e3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.cloudconnection.request.RequestHandler;
import org.eclipse.kura.cloudconnection.request.RequestHandlerRegistry;
import org.eclipse.kura.configuration.ConfigurationService;
Expand Down Expand Up @@ -138,6 +141,12 @@ public UserDTO getUser(final UserDTO userName) {
try {
logger.debug(DEBUG_MESSAGE, "getUser");
return this.identityService.getUser(userName.getUserName());
} catch (KuraException e) {
if (e.getCode().equals(KuraErrorCode.NOT_FOUND)) {
throw DefaultExceptionHandler.buildWebApplicationException(Status.NOT_FOUND, "Identity does not exist");
} else {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
} catch (Exception e) {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
Expand All @@ -152,6 +161,12 @@ public Response deleteUser(final UserDTO userName) {
try {
logger.debug(DEBUG_MESSAGE, "deleteUser");
this.identityService.deleteUser(userName.getUserName());
} catch (KuraException e) {
if (e.getCode().equals(KuraErrorCode.NOT_FOUND)) {
throw DefaultExceptionHandler.buildWebApplicationException(Status.NOT_FOUND, "Identity does not exist");
} else {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
} catch (Exception e) {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@
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 @@ -81,24 +77,24 @@ public void createUser(UserDTO user) throws KuraException {
}
}

public void deleteUser(String userName) throws WebApplicationException {
public void deleteUser(String userName) throws KuraException {

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

}

public UserDTO getUser(String userName) throws WebApplicationException {
public UserDTO getUser(String userName) throws KuraException {
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 DefaultExceptionHandler.buildWebApplicationException(Status.NOT_FOUND, "Identity does not exist");
throw new KuraException(KuraErrorCode.NOT_FOUND, "Identity does not exist");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
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 @@ -121,7 +119,7 @@ public void shoulGetUser() {
private void whenGettingUser(String username) {
try {
this.user = this.identityService.getUser(username);
} catch (WebApplicationException e) {
} catch (KuraException e) {
this.occurredException = e;
}
}
Expand Down

0 comments on commit a3d85e3

Please sign in to comment.