From a367cd6b359f1310000c845c01aadd677ab5d9ce Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Mon, 5 Dec 2022 13:11:37 +0530 Subject: [PATCH] Fix duplicate group and role error handling responses --- .../impl/RoleManagementServiceImpl.java | 27 +++++++++++++------ .../GroupManagementProviderServiceImpl.java | 14 ++++++---- .../api/service/TraccarClientFactory.java | 2 +- .../api/service/addons/TraccarClientImpl.java | 2 +- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java index 7bedb0f890..af05da7991 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/RoleManagementServiceImpl.java @@ -54,6 +54,7 @@ import org.wso2.carbon.registry.core.session.UserRegistry; import org.wso2.carbon.registry.resource.services.utils.ChangeRolePermissionsUtil; import org.wso2.carbon.user.api.*; import org.wso2.carbon.user.core.common.AbstractUserStoreManager; +import org.wso2.carbon.user.core.constants.UserCoreErrorConstants.ErrorMessages; import org.wso2.carbon.user.mgt.UserRealmProxy; import org.wso2.carbon.user.mgt.common.UIPermissionNode; import org.wso2.carbon.user.mgt.common.UserAdminException; @@ -316,21 +317,31 @@ public class RoleManagementServiceImpl implements RoleManagementService { entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been" + " added").build(); } catch (UserStoreException e) { - String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'"; - log.error(msg, e); - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + String errorCode = ""; + String errorMessage = e.getMessage(); + if (errorMessage != null && !errorMessage.isEmpty() && + errorMessage.contains(ErrorMessages.ERROR_CODE_ROLE_ALREADY_EXISTS.getCode())) { + errorCode = e.getMessage().split("-")[0].trim(); + } + if (ErrorMessages.ERROR_CODE_ROLE_ALREADY_EXISTS.getCode().equals(errorCode)) { + String roleName = roleInfo.getRoleName().split("/")[1]; + String msg = "Role already exists with name " + roleName + "."; + log.warn(msg); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); + } else { + String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } } catch (URISyntaxException e) { String msg = "Error occurred while composing the URI at which the information of the newly created role " + "can be retrieved"; log.error(msg, e); - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (UnsupportedEncodingException e) { String msg = "Error occurred while encoding role name"; log.error(msg, e); - return Response.serverError().entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 66a8ba9362..aed9bc8782 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -47,6 +47,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; +import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; @@ -140,12 +141,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (HttpReportingUtil.isTrackerEnabled()){ existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); int groupId = existingGroup.getGroupId(); - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() - .addGroup(deviceGroup, groupId, tenantId); + try { + DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() + .addGroup(deviceGroup, groupId, tenantId); + } catch (TrackerAlreadyExistException e) { + throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); + } + } else { + throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); } - // add a group if not exist in traccar starts - - throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); } } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java index bffa6f5e85..2a94596230 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java @@ -648,7 +648,7 @@ public class TraccarClientFactory { TrackerManagementDAOFactory.openConnection(); trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); if (trackerGroupInfo != null) { - String msg = "The group already exit"; + String msg = "The group already exists in Traccar."; log.error(msg); throw new TrackerAlreadyExistException(msg); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java index 0e9264a325..efa13ee6a7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java @@ -555,7 +555,7 @@ public class TraccarClientImpl implements TraccarClient { TrackerManagementDAOFactory.openConnection(); trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); if (trackerGroupInfo != null) { - String msg = "The group already exit"; + String msg = "The group already exists in Traccar."; log.error(msg); throw new TrackerAlreadyExistException(msg); }