Fix duplicate group and role error handling responses

master
commit 6d72292789

@ -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.registry.resource.services.utils.ChangeRolePermissionsUtil;
import org.wso2.carbon.user.api.*; import org.wso2.carbon.user.api.*;
import org.wso2.carbon.user.core.common.AbstractUserStoreManager; 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.UserRealmProxy;
import org.wso2.carbon.user.mgt.common.UIPermissionNode; import org.wso2.carbon.user.mgt.common.UIPermissionNode;
import org.wso2.carbon.user.mgt.common.UserAdminException; import org.wso2.carbon.user.mgt.common.UserAdminException;
@ -316,21 +317,31 @@ public class RoleManagementServiceImpl implements RoleManagementService {
entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been" entity("Role '" + roleInfo.getRoleName() + "' has " + "successfully been"
+ " added").build(); + " added").build();
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occurred while adding role '" + roleInfo.getRoleName() + "'"; String errorCode = "";
log.error(msg, e); String errorMessage = e.getMessage();
return Response.serverError().entity( if (errorMessage != null && !errorMessage.isEmpty() &&
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); 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) { } catch (URISyntaxException e) {
String msg = "Error occurred while composing the URI at which the information of the newly created role " + String msg = "Error occurred while composing the URI at which the information of the newly created role " +
"can be retrieved"; "can be retrieved";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
String msg = "Error occurred while encoding role name"; String msg = "Error occurred while encoding role name";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }

@ -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.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; 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.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
@ -140,12 +141,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (HttpReportingUtil.isTrackerEnabled()){ if (HttpReportingUtil.isTrackerEnabled()){
existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId);
int groupId = existingGroup.getGroupId(); int groupId = existingGroup.getGroupId();
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() try {
.addGroup(deviceGroup, groupId, tenantId); 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) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();

@ -648,7 +648,7 @@ public class TraccarClientFactory {
TrackerManagementDAOFactory.openConnection(); TrackerManagementDAOFactory.openConnection();
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
if (trackerGroupInfo != null) { if (trackerGroupInfo != null) {
String msg = "The group already exit"; String msg = "The group already exists in Traccar.";
log.error(msg); log.error(msg);
throw new TrackerAlreadyExistException(msg); throw new TrackerAlreadyExistException(msg);
} }

@ -555,7 +555,7 @@ public class TraccarClientImpl implements TraccarClient {
TrackerManagementDAOFactory.openConnection(); TrackerManagementDAOFactory.openConnection();
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
if (trackerGroupInfo != null) { if (trackerGroupInfo != null) {
String msg = "The group already exit"; String msg = "The group already exists in Traccar.";
log.error(msg); log.error(msg);
throw new TrackerAlreadyExistException(msg); throw new TrackerAlreadyExistException(msg);
} }

Loading…
Cancel
Save