Skip to content

Commit

Permalink
Merge pull request #12570 from tharikaGitHub/error-responses-patch-port
Browse files Browse the repository at this point in the history
Improve product rest api error responses for publisher
  • Loading branch information
tharikaGitHub committed Sep 20, 2024
2 parents a1bcf9d + 499e3be commit faa4667
Show file tree
Hide file tree
Showing 13 changed files with 310 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,111 @@ public enum ExceptionCodes implements ErrorHandler {
ENDPOINT_SECURITY_TYPE_NOT_DEFINED(903214, "Endpoint security type not defined", 400,
"Endpoint security type not defined for the %s endpoint", false),

ADDITIONAL_PROPERTIES_CANNOT_BE_NULL(903215, "'additionalProperties' is required and should " +
"not be null", 400,
"The field 'additionalProperties' is required and should not be null"),

ADDITIONAL_PROPERTIES_PARSE_ERROR(903216, "Error while parsing 'additionalProperties'", 400,
"Error while parsing 'additionalProperties'", true),

ENDPOINT_SECURITY_CRYPTO_EXCEPTION(903217, "Error while encrypting the secret key of API", 500,
"%s"),

OPENAPI_RETRIEVAL_ERROR(903218, "Error while retrieving the OAS definition", 500,
"Error while retrieving the OAS definition for API with UUID %s"),

ASYNCAPI_RETRIEVAL_ERROR(903219, "Error while retrieving the Async API definition", 500,
"Error while retrieving the Async API definition for API with UUID %s"),

ERROR_RETRIEVING_API(903220, "Failed to get API", 500, "Failed to get API with UUID %s"),

ERROR_CHANGING_REGISTRY_LIFECYCLE_STATE(903221, "Error changing registry lifecycle state", 500,
"Error changing registry lifecycle state for API/API Product with UUID %s"),

UN_AUTHORIZED_TO_VIEW_MODIFY_API(903222, "User is not authorized to view or modify the api",
403, "User %s is not authorized to view or modify the api"),

FAILED_PUBLISHING_API_NO_ENDPOINT_SELECTED(903223, "Failed to publish service to API store. No endpoint selected",
400, "Failed to publish service to API store. No endpoint selected for API with UUID %s"),

FAILED_PUBLISHING_API_NO_TIERS_SELECTED(903224, "Failed to publish service to API store. No Tiers selected",
400, "Failed to publish service to API store. No Tiers selected for API with UUID %s"),

THIRD_PARTY_API_REVISION_CREATION_UNSUPPORTED(903225, "Creating API Revisions is not supported " +
"for third party APIs", 400,"Creating API Revisions is not supported for third party APIs: %s"),

THIRD_PARTY_API_REVISION_DEPLOYMENT_UNSUPPORTED(903226, "Deploying API Revisions is not supported " +
"for third party APIs", 400,"Deploying API Revisions is not supported for third party APIs: %s"),

RETIRED_API_REVISION_DEPLOYMENT_UNSUPPORTED(903227, "Deploying API Revisions is not supported for retired APIs",
400, "Deploying API Revisions is not supported for retired APIs. ApiId: %s"),

REVISION_NOT_FOUND_FOR_REVISION_NUMBER(903228, "No revision found", 404,
"No revision found for revision number %s"),

ERROR_PROCESSING_DIRECTORY_TO_IMPORT(903229, "Error extracting and processing the directory", 500,
"Error extracting and processing the directory to be imported", true),

IMPORT_ERROR_INVALID_GRAPHQL_SCHEMA(903230, "Error occurred while importing the API. Invalid " +
"GraphQL schema definition found", 400, "Invalid GraphQL schema definition " +
"found. %s"),

IMPORT_ERROR_INVALID_ASYNC_API_SCHEMA(903231, "Error occurred while importing the API. " +
"Invalid AsyncAPI definition found.", 400, "Invalid AsyncAPI definition found. %s"),

NO_VHOSTS_DEFINED_FOR_ENVIRONMENT(903232, "No VHosts defined for the environment", 400,
"No VHosts defined for the environment: %s"),

PROVIDED_GATEWAY_ENVIRONMENT_NOT_FOUND(903233, "Gateway environment not found", 400,
"Provided gateway environment %s is not found"),

UNSUPPORTED_AND_ALLOWED_LIFECYCLE_ACTIONS(903234, "Unsupported state change action", 400,
"Lifecycle state change action %s is not allowed for this API. Allowed actions are %s"),

NO_CORRESPONDING_RESOURCE_FOUND_IN_API(903235, "No corresponding resource found in API", 400,
"API with id %s does not have a resource %s with http method %s"),

ERROR_PARSING_MONETIZATION_PROPERTIES(903237, "Error when parsing monetization properties",
400, "Error when parsing monetization properties"),

API_NAME_CANNOT_BE_NULL(903238, "API name is required", 400,
"API name is required and cannot be null"),

API_NAME_ILLEGAL_CHARACTERS(903239, "API name contains illegal characters", 400,
"API name %s contains one or more illegal characters from (%s)"),

API_VERSION_CANNOT_BE_NULL(903240, "API version is required", 400,
"API version is required and cannot be null"),

API_VERSION_ILLEGAL_CHARACTERS(903241, "API version contains illegal characters", 400,
"API version %s contains one or more illegal characters from (%s)"),

UNSUPPORTED_CONTEXT(903242, "Unsupported context", 400,
"Unsupported context %s"),

ERROR_PARSING_ENDPOINT_CONFIG(903243, "Error when parsing endpoint configuration",
400, "Error when parsing endpoint configuration"),

NOT_IN_OPEN_API_FORMAT(903244, "Not in Open API format",
400, "The API definition is not in Open API format"),

PARAMETER_NOT_PROVIDED_FOR_DOCUMENTATION(903245, "Parameter value missing", 400,
"Some of the mandatory parameter values were missing. %s"),

INVALID_API_RESOURCES_FOR_API_PRODUCT(903246, "Cannot find API resources for some API Product " +
"resources.", 404, "Some of the resources in the API Product are not found as API resources. %s"),

INVALID_ADDITIONAL_PROPERTIES_WITH_ERROR(903247, "Invalid additional properties", 400,
"Invalid additional properties for API: %s:%s Error: %s"),

TIER_NAME_INVALID_WITH_TIER_INFO(903248, "The tier name is invalid.", 400,
"The tier name(s) %s are invalid"),

LENGTH_EXCEEDS_ERROR(903249, "Character length exceeds the allowable limit", 400, "%s"),

ROLE_OF_SCOPE_DOES_NOT_EXIST(903250, "Role does not exist", 404,
"Role %s does not exist"),

OPERATION_OR_RESOURCE_TYPE_OR_METHOD_NOT_DEFINED(902031,
"Operation type/http method is not specified for the operation/resource", 400,
"Operation type/http method is not specified for the operation/resource: %s", false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2041,8 +2041,10 @@ public API createNewAPIVersion(String existingApiId, String newVersion, Boolean
ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, existingApiId));
}
if (newVersion.equals(existingAPI.getId().getVersion())) {
throw new APIMgtResourceAlreadyExistsException(
"Version " + newVersion + " exists for api " + existingAPI.getId().getApiName());
String errorMessage = "Version " + newVersion + " exists for api " + existingAPI.getId().getApiName();
throw new APIMgtResourceAlreadyExistsException(errorMessage,
ExceptionCodes.from(ExceptionCodes.API_VERSION_ALREADY_EXISTS, newVersion,
existingAPI.getId().getApiName()));
}
if (APIUtil.isSequenceDefined(existingAPI.getInSequence()) || APIUtil.isSequenceDefined(existingAPI.getOutSequence())
|| APIUtil.isSequenceDefined(existingAPI.getFaultSequence())) {
Expand Down Expand Up @@ -3111,6 +3113,7 @@ public void updateAPIProductSwagger(String productId, Map<API,
public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper apiTypeWrapper, String action,
Map<String, Boolean> checklist) throws APIManagementException{
APIStateChangeResponse response = new APIStateChangeResponse();
String uuid = null;
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
Expand All @@ -3123,7 +3126,6 @@ public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper
String apiType;
String apiVersion;
String currentStatus;
String uuid;
int apiOrApiProductId;
boolean isApiProduct = apiTypeWrapper.isAPIProduct();
String workflowType;
Expand Down Expand Up @@ -3188,7 +3190,8 @@ public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper
APIConstants.AuditLogConstants.LIFECYCLE_CHANGED, this.username);
}
} catch (APIPersistenceException e) {
handleException("Error while accessing persistence layer", e);
throw new APIManagementException("Error while accessing persistence layer", e,
ExceptionCodes.from(ExceptionCodes.ERROR_CHANGING_REGISTRY_LIFECYCLE_STATE, uuid));
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
Expand Down Expand Up @@ -4423,9 +4426,10 @@ private static void validateAPIProductContextTemplate(APIProduct product) throws
//Validate if the API Product has an unsupported context before executing the query
String invalidContext = "/" + APIConstants.VERSION_PLACEHOLDER;
if (invalidContext.equals(contextTemplate)) {
throw new APIManagementException(
"Cannot add API Product : " + product.getId() + " with unsupported context : "
+ contextTemplate);
String errorMessage = "Cannot add API Product : " + product.getId() + " with unsupported context : "
+ contextTemplate;
throw new APIManagementException(errorMessage,
ExceptionCodes.from(ExceptionCodes.UNSUPPORTED_CONTEXT, contextTemplate));
}
}

Expand Down Expand Up @@ -4593,9 +4597,13 @@ public Map<API, List<APIProductResource>> updateAPIProduct(APIProduct product)
uriTemplate.setId(templateMap.get(key).getId());

} else {
throw new APIManagementException("API with id " + apiProductResource.getApiId()
String errorMessage = "API with id " + apiProductResource.getApiId()
+ " does not have a resource " + uriTemplate.getUriTemplate()
+ " with http method " + uriTemplate.getHTTPVerb());
+ " with http method " + uriTemplate.getHTTPVerb();
throw new APIManagementException(errorMessage,
ExceptionCodes.from(ExceptionCodes.NO_CORRESPONDING_RESOURCE_FOUND_IN_API,
apiProductResource.getApiId(), uriTemplate.getUriTemplate(),
uriTemplate.getHTTPVerb()));
}
}
}
Expand All @@ -4620,7 +4628,9 @@ public Map<API, List<APIProductResource>> updateAPIProduct(APIProduct product)
JSONObject jsonObj = (JSONObject) parser.parse(gson.toJson(newMonetizationProperties));
product.setMonetizationProperties(jsonObj);
} catch (ParseException e) {
throw new APIManagementException("Error when parsing monetization properties ", e);
String errorMessage = "Error when parsing monetization properties ";
throw new APIManagementException(errorMessage, e,
ExceptionCodes.ERROR_PARSING_MONETIZATION_PROPERTIES);
}
}
}
Expand Down Expand Up @@ -4696,18 +4706,24 @@ private void validateApiLifeCycleForApiProducts(API api) throws APIManagementExc
private void validateApiProductInfo(APIProduct product) throws APIManagementException {
String apiName = product.getId().getName();
if (apiName == null) {
handleException("API Name is required.");
throw new APIManagementException("API Name is required.", ExceptionCodes.API_NAME_CANNOT_BE_NULL);
} else if (containsIllegals(apiName)) {
handleException("API Name contains one or more illegal characters " +
"( " + APIConstants.REGEX_ILLEGAL_CHARACTERS_FOR_API_METADATA + " )");
String errorMessage = "API Name contains one or more illegal characters ( "
+ APIConstants.REGEX_ILLEGAL_CHARACTERS_FOR_API_METADATA + " )";
throw new APIManagementException(errorMessage,
ExceptionCodes.from(ExceptionCodes.API_NAME_ILLEGAL_CHARACTERS, apiName,
APIConstants.REGEX_ILLEGAL_CHARACTERS_FOR_API_METADATA));
}
String apiVersion = product.getId().getVersion();

if (apiVersion == null) {
handleException("API Version is required.");
throw new APIManagementException("API Version is required.", ExceptionCodes.API_VERSION_CANNOT_BE_NULL);
} else if (containsIllegals(apiVersion)) {
handleException("API Version contains one or more illegal characters " +
"( " + APIConstants.REGEX_ILLEGAL_CHARACTERS_FOR_API_METADATA + " )");
String errorMessage = "API Version contains one or more illegal characters ( "
+ APIConstants.REGEX_ILLEGAL_CHARACTERS_FOR_API_METADATA + " )";
throw new APIManagementException(errorMessage,
ExceptionCodes.from(ExceptionCodes.API_VERSION_ILLEGAL_CHARACTERS, apiVersion,
APIConstants.REGEX_ILLEGAL_CHARACTERS_FOR_API_METADATA));
}

if (!hasValidLength(apiName, APIConstants.MAX_LENGTH_API_NAME)
Expand Down Expand Up @@ -5255,13 +5271,17 @@ public API getAPIbyUUID(String uuid, String organization) throws APIManagementEx
throw new APIMgtResourceNotFoundException(msg);
}
} catch (APIPersistenceException e) {
throw new APIManagementException("Failed to get API", e);
throw new APIManagementException("Failed to get API", e,
ExceptionCodes.from(ExceptionCodes.ERROR_RETRIEVING_API, uuid));
} catch (OASPersistenceException e) {
throw new APIManagementException("Error while retrieving the OAS definition", e);
throw new APIManagementException("Error while retrieving the OAS definition", e,
ExceptionCodes.from(ExceptionCodes.OPENAPI_RETRIEVAL_ERROR, uuid));
} catch (ParseException e) {
throw new APIManagementException("Error while parsing the OAS definition", e);
throw new APIManagementException("Error while parsing the OAS definition", e,
ExceptionCodes.from(ExceptionCodes.OPENAPI_PARSE_EXCEPTION, uuid));
} catch (AsyncSpecPersistenceException e) {
throw new APIManagementException("Error while retrieving the Async API definition", e);
throw new APIManagementException("Error while retrieving the Async API definition", e,
ExceptionCodes.from(ExceptionCodes.ASYNCAPI_RETRIEVAL_ERROR, uuid));
}
}

Expand Down Expand Up @@ -5526,7 +5546,7 @@ public API getLightweightAPIByUUID(String uuid, String organization) throws APIM
}
} catch (APIPersistenceException e) {
String msg = "Failed to get API with uuid " + uuid;
throw new APIManagementException(msg, e);
throw new APIManagementException(msg, e, ExceptionCodes.from(ExceptionCodes.ERROR_RETRIEVING_API, uuid));
}
}

Expand Down Expand Up @@ -5765,8 +5785,9 @@ protected void checkAccessControlPermission(String userNameWithTenantDomain, Str
return;
}
}

throw new APIManagementException(APIConstants.UN_AUTHORIZED_ERROR_MESSAGE + " view or modify the api");
String errorMessage = APIConstants.UN_AUTHORIZED_ERROR_MESSAGE + " view or modify the api";
throw new APIManagementException(errorMessage,
ExceptionCodes.from(ExceptionCodes.UN_AUTHORIZED_TO_VIEW_MODIFY_API, username));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ public static String updateAPIProductSwaggerOperations(Map<API, List<APIProductR
if (destinationSwaggerVersion == SwaggerVersion.OPEN_API) {
destOpenAPI = ((OAS3Parser) oas3Parser).getOpenAPI(destinationSwagger);
} else {
throw new APIManagementException("Cannot update destination swagger because it is not in OpenAPI format");
String errorMessage = "Cannot update destination swagger because it is not in OpenAPI format";
throw new APIManagementException(errorMessage, ExceptionCodes.NOT_IN_OPEN_API_FORMAT);
}

SwaggerUpdateContext context = new SwaggerUpdateContext();
Expand Down
Loading

0 comments on commit faa4667

Please sign in to comment.