From 6eaa6c4ad394dc8cd3ee8d6ea2a77c03c23d9dc8 Mon Sep 17 00:00:00 2001 From: hiranyakavishani Date: Wed, 13 Mar 2024 09:50:03 +0530 Subject: [PATCH] Logging error msg for register transactions when adding/updating API/APIProducts --- .../carbon/apimgt/impl/APIProviderImpl.java | 8 +-- .../persistence/RegistryPersistenceImpl.java | 67 +++++++++++-------- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index b5cba294f1c0..4139851a488f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -429,7 +429,7 @@ public API addAPI(API api) throws APIManagementException { api.setUuid(addedAPI.getId()); api.setCreatedTime(addedAPI.getCreatedTime()); } catch (APIPersistenceException e) { - throw new APIManagementException("Error while persisting API ", e); + throw new APIManagementException("Error while persisting API. " + e.getMessage(), e); } if (log.isDebugEnabled()) { @@ -866,7 +866,7 @@ public API updateAPI(API api, API existingAPI) throws APIManagementException { api.setCreatedTime(existingAPI.getCreatedTime()); apiPersistenceInstance.updateAPI(new Organization(organization), APIMapper.INSTANCE.toPublisherApi(api)); } catch (APIPersistenceException e) { - throw new APIManagementException("Error while updating API details", e); + throw new APIManagementException("Error while updating API details. " + e.getMessage(), e); } APIUtil.logAuditMessage(APIConstants.AuditLogConstants.API, apiLogObject.toString(), APIConstants.AuditLogConstants.UPDATED, this.username); @@ -4533,7 +4533,7 @@ protected String createAPIProduct(APIProduct apiProduct) throws APIManagementExc apiProductUUID = addedAPIProduct.getId(); } catch (APIPersistenceException e) { - throw new APIManagementException("Error while creating API product ", e); + throw new APIManagementException("Error while creating API product. " + e.getMessage(), e); } @@ -4566,7 +4566,7 @@ private void updateApiProductArtifact(APIProduct apiProduct, boolean updateMetad new Organization(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()), publisherAPIProduct); } catch (APIPersistenceException e) { - throw new APIManagementException("Error while creating API product "); + throw new APIManagementException("Error while creating API product. " + e.getMessage()); } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java index 45734e031436..20494d0a4e15 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/RegistryPersistenceImpl.java @@ -161,12 +161,7 @@ public PublisherAPI addAPI(Organization org, PublisherAPI publisherAPI) throws A String providerPath = RegistryPersistenceUtil.getAPIProviderPath(api.getId()); //provider ------provides----> API registry.addAssociation(providerPath, artifactPath, APIConstants.PROVIDER_ASSOCIATION); - Set tagSet = api.getTags(); - if (tagSet != null) { - for (String tag : tagSet) { - registry.applyTag(artifactPath, tag); - } - } + applyTags(api.getTags(), registry, artifactPath); String apiStatus = api.getStatus(); saveAPIStatus(registry, artifactPath, apiStatus); @@ -248,7 +243,8 @@ public PublisherAPI addAPI(Organization org, PublisherAPI publisherAPI) throws A registry.rollbackTransaction(); } catch (RegistryException re) { // Throwing an error here would mask the original exception - log.error("Error while rolling back the transaction for API: " + api.getId().getApiName(), re); + log.error("Error while rolling back the transaction for API: " + api.getId() + .getApiName() + ". " + e.getMessage(), re); } throw new APIPersistenceException("Error while performing registry transaction operation", e); } catch (APIManagementException e) { @@ -267,6 +263,32 @@ public PublisherAPI addAPI(Organization org, PublisherAPI publisherAPI) throws A } } + private void applyTags(Set apiTags, Registry registry, String artifactPath) + throws APIPersistenceException { + Set tagSet = apiTags; + if (tagSet != null) { + for (String tag : tagSet) { + try { + registry.applyTag(artifactPath, tag); + } catch (RegistryException e) { + try { + registry.rollbackTransaction(); + } catch (RegistryException re) { + log.error( + "Error while rolling back the transaction of API Tag: " + tag + " to the artifact: " + + artifactPath + ". " + e.getMessage(), re); + } + + String illegalCharacterString = "illegal characters"; + if (e.getMessage().contains(illegalCharacterString)) { + throw new APIPersistenceException(e.getMessage()); + } + throw new APIPersistenceException("Error while performing registry transaction operation", e); + } + } + } + } + @Override public String addAPIRevision(Organization org, String apiUUID, int revisionId) throws APIPersistenceException { String revisionUUID; @@ -512,12 +534,7 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw registry.removeTag(artifactPath, tag.getTagName()); } } - Set tagSet = api.getTags(); - if (tagSet != null) { - for (String tag : tagSet) { - registry.applyTag(artifactPath, tag); - } - } + applyTags(api.getTags(), registry, artifactPath); artifactManager.updateGenericArtifact(updateApiArtifact); //write API Status to a separate property. This is done to support querying APIs using custom query (SQL) @@ -628,7 +645,8 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw registry.rollbackTransaction(); } catch (RegistryException re) { // Throwing an error from this level will mask the original exception - log.error("Error while rolling back the transaction for API: " + api.getId().getApiName(), re); + log.error("Error while rolling back the transaction for API: " + api.getId().getApiName() + + ". " + e.getMessage(), re); } throw new APIPersistenceException("Error while performing registry transaction operation ", e); } finally { @@ -3216,13 +3234,7 @@ public PublisherAPIProduct addAPIProduct(Organization org, PublisherAPIProduct p String apiProductStatus = apiProduct.getState(); saveAPIStatus(registry, artifactPath, apiProductStatus); - - Set tagSet = apiProduct.getTags(); - if (tagSet != null) { - for (String tag : tagSet) { - registry.applyTag(artifactPath, tag); - } - } + applyTags(apiProduct.getTags(), registry, artifactPath); String visibleRolesList = apiProduct.getVisibleRoles(); String[] visibleRoles = new String[0]; @@ -3255,7 +3267,7 @@ public PublisherAPIProduct addAPIProduct(Organization org, PublisherAPIProduct p } catch (RegistryException re) { // Throwing an error here would mask the original exception log.error("Error while rolling back the transaction for API Product : " - + publisherAPIProduct.getApiProductName(), re); + + publisherAPIProduct.getApiProductName() + ". " + e.getMessage(), re); } throw new APIPersistenceException("Error while performing registry transaction operation", e); } catch (APIManagementException e) { @@ -3449,12 +3461,8 @@ public PublisherAPIProduct updateAPIProduct(Organization org, PublisherAPIProduc registry.removeTag(artifactPath, tag.getTagName()); } } - Set tagSet = apiProduct.getTags(); - if (tagSet != null) { - for (String tag : tagSet) { - registry.applyTag(artifactPath, tag); - } - } + + applyTags(apiProduct.getTags(), registry, artifactPath); String publisherAccessControlRoles = apiProduct.getAccessControlRoles(); updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(), @@ -3479,7 +3487,8 @@ public PublisherAPIProduct updateAPIProduct(Organization org, PublisherAPIProduc registry.rollbackTransaction(); } } catch (RegistryException ex) { - log.error("Error occurred while rolling back the transaction.", ex); + log.error("Error while rolling back the transaction for API Product : " + + publisherAPIProduct.getApiProductName() + ". " + ex.getMessage(), ex); } if (isTenantFlowStarted) { PrivilegedCarbonContext.endTenantFlow();