Skip to content

Commit

Permalink
Logging error msg for register transactions when adding/updating API/…
Browse files Browse the repository at this point in the history
…APIProducts
  • Loading branch information
HiranyaKavishani committed Mar 13, 2024
1 parent c4c0594 commit 6eaa6c4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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);
Expand Down Expand Up @@ -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) {
Expand All @@ -267,6 +263,32 @@ public PublisherAPI addAPI(Organization org, PublisherAPI publisherAPI) throws A
}
}

private void applyTags(Set<String> apiTags, Registry registry, String artifactPath)
throws APIPersistenceException {
Set<String> 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;
Expand Down Expand Up @@ -512,12 +534,7 @@ public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throw
registry.removeTag(artifactPath, tag.getTagName());
}
}
Set<String> 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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -3216,13 +3234,7 @@ public PublisherAPIProduct addAPIProduct(Organization org, PublisherAPIProduct p

String apiProductStatus = apiProduct.getState();
saveAPIStatus(registry, artifactPath, apiProductStatus);

Set<String> 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];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -3449,12 +3461,8 @@ public PublisherAPIProduct updateAPIProduct(Organization org, PublisherAPIProduc
registry.removeTag(artifactPath, tag.getTagName());
}
}
Set<String> 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(),
Expand All @@ -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();
Expand Down

0 comments on commit 6eaa6c4

Please sign in to comment.