RANGER-4727: failure to delete tagDef should return appropriate error message

Signed-off-by: Madhan Neethiraj <madhan@apache.org>
diff --git a/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java b/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
index fb912d4..a472fe1 100644
--- a/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
+++ b/security-admin/src/main/java/org/apache/ranger/biz/TagDBStore.java
@@ -165,23 +165,15 @@
 	@Override
 	public void deleteTagDefByName(String name) throws Exception {
 		if (LOG.isDebugEnabled()) {
-			LOG.debug("==> TagDBStore.deleteTagDef(" + name + ")");
+			LOG.debug("==> TagDBStore.deleteTagDefByName(" + name + ")");
 		}
 
 		if (StringUtils.isNotBlank(name)) {
-			RangerTagDef tagDef = getTagDefByName(name);
-
-			if(tagDef != null) {
-				if(LOG.isDebugEnabled()) {
-					LOG.debug("Deleting tag-def [name=" + name + "; id=" + tagDef.getId() + "]");
-				}
-
-				rangerTagDefService.delete(tagDef);
-			}
+			deleteTagDef(getTagDefByName(name));
 		}
 
 		if (LOG.isDebugEnabled()) {
-			LOG.debug("<== TagDBStore.deleteTagDef(" + name + ")");
+			LOG.debug("<== TagDBStore.deleteTagDefByName(" + name + ")");
 		}
 	}
 
@@ -192,11 +184,7 @@
 		}
 
 		if(id != null) {
-			RangerTagDef tagDef = rangerTagDefService.read(id);
-
-			if(tagDef != null) {
-				rangerTagDefService.delete(tagDef);
-			}
+			deleteTagDef(rangerTagDefService.read(id));
 		}
 
 		if (LOG.isDebugEnabled()) {
@@ -1382,4 +1370,20 @@
 		initStatics();
 		return SUPPORTS_IN_PLACE_TAG_UPDATES;
 	}
+
+	private void deleteTagDef(RangerTagDef tagDef) throws Exception {
+		if (tagDef != null) {
+			if (LOG.isDebugEnabled()) {
+				LOG.debug("Deleting tag-def [name=" + tagDef.getName() + "; id=" + tagDef.getId() + "]");
+			}
+
+			List<RangerTag> tagsByType = rangerTagService.getTagsByType(tagDef.getName());
+
+			if (CollectionUtils.isEmpty(tagsByType)) {
+				rangerTagDefService.delete(tagDef);
+			} else {
+				throw new Exception("Cannot delete tag-def: " + tagDef.getName() + ". " + tagsByType.size() + " tag instances for this tag-def exist");
+			}
+		}
+	}
 }
diff --git a/security-admin/src/main/java/org/apache/ranger/common/RESTErrorUtil.java b/security-admin/src/main/java/org/apache/ranger/common/RESTErrorUtil.java
index c3af9f9..50e13e0 100644
--- a/security-admin/src/main/java/org/apache/ranger/common/RESTErrorUtil.java
+++ b/security-admin/src/main/java/org/apache/ranger/common/RESTErrorUtil.java
@@ -352,8 +352,12 @@
 
 	public WebApplicationException createRESTException(int responseCode,
 			String logMessage, boolean logError) {
+		VXResponse response = new VXResponse();
+
+		response.setMsgDesc(logMessage);
+
 		Response errorResponse = Response
-				.status(responseCode).entity(logMessage).build();
+				.status(responseCode).entity(response).build();
 
 		WebApplicationException restException = new WebApplicationException(
 				errorResponse);