com.plenigo.sdk.internal.exceptions.ApiExceptionTranslator Maven / Gradle / Ivy
package com.plenigo.sdk.internal.exceptions;
import com.plenigo.sdk.PlenigoException;
import com.plenigo.sdk.internal.ApiURLs;
import com.plenigo.sdk.internal.ErrorCode;
import com.plenigo.sdk.internal.util.SdkUtils;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* This translates error responses that can come from
* the plenigo API or SDK into plenigo exceptions.
*
*
* IMPORTANT: This class is part of the internal API, please do not use it, because it can
* be removed in future versions of the SDK or access to such elements could
* be changed from 'public' to 'default' or less.
*
*
* Thread safety: This class is thread safe and can be injected.
*
*/
public final class ApiExceptionTranslator {
private static final Logger LOGGER = Logger.getLogger(ApiExceptionTranslator.class.getName());
private static final ApiExceptionTranslator INSTANCE = new ApiExceptionTranslator();
private Map> errorCodes;
private ErrorHandler defaultHandler = new DefaultHandler();
/**
* Default constructor.
*/
private ApiExceptionTranslator() {
LOGGER.log(Level.FINEST, "Registering error codes...");
errorCodes = new HashMap>();
Map productApiCodes = new LinkedHashMap();
productApiCodes.put(String.valueOf(HttpURLConnection.HTTP_FORBIDDEN), new ApiExceptionInfo(ErrorCode.CANNOT_ACCESS_PRODUCT));
productApiCodes.put(String.valueOf(HttpURLConnection.HTTP_UNAUTHORIZED), new ApiExceptionInfo(ErrorCode.INVALID_SECRET_OR_COMPANY_ID));
productApiCodes.put(String.valueOf(HttpURLConnection.HTTP_BAD_REQUEST), new ApiExceptionInfo(ErrorCode.INVALID_PARAMETERS,
new InvalidParametersHandler()));
errorCodes.put(ApiURLs.USER_PRODUCT_ACCESS, productApiCodes);
Map queryProductApiCodes = new LinkedHashMap();
queryProductApiCodes.put(String.valueOf(HttpURLConnection.HTTP_NOT_FOUND), new ApiExceptionInfo(ErrorCode.PRODUCT_NOT_FOUND));
queryProductApiCodes.put(String.valueOf(HttpURLConnection.HTTP_UNAUTHORIZED), new ApiExceptionInfo(ErrorCode.INVALID_SECRET_OR_COMPANY_ID));
queryProductApiCodes.put(String.valueOf(HttpURLConnection.HTTP_BAD_REQUEST), new ApiExceptionInfo(ErrorCode.INVALID_PARAMETERS,
new InvalidParametersHandler()));
errorCodes.put(ApiURLs.GET_PRODUCT, queryProductApiCodes);
Map paywallStateApiCodes = new LinkedHashMap();
paywallStateApiCodes.put(String.valueOf(HttpURLConnection.HTTP_UNAUTHORIZED), new ApiExceptionInfo(ErrorCode.INVALID_SECRET_OR_COMPANY_ID));
paywallStateApiCodes.put(String.valueOf(HttpURLConnection.HTTP_BAD_REQUEST), new ApiExceptionInfo(ErrorCode.INVALID_PARAMETERS,
new InvalidParametersHandler()));
errorCodes.put(ApiURLs.PAYWALL_STATE, paywallStateApiCodes);
Map productListApiCodes = new LinkedHashMap();
productListApiCodes.put(String.valueOf(HttpURLConnection.HTTP_UNAUTHORIZED), new ApiExceptionInfo(ErrorCode.INVALID_SECRET_OR_COMPANY_ID));
productListApiCodes.put(String.valueOf(HttpURLConnection.HTTP_BAD_REQUEST), new ApiExceptionInfo(ErrorCode.INVALID_PARAMETERS,
new InvalidParametersHandler()));
errorCodes.put(ApiURLs.LIST_PRODUCTS, productListApiCodes);
Map categoryListApiCodes = new LinkedHashMap();
categoryListApiCodes.put(String.valueOf(HttpURLConnection.HTTP_UNAUTHORIZED), new ApiExceptionInfo(ErrorCode.INVALID_SECRET_OR_COMPANY_ID));
categoryListApiCodes.put(String.valueOf(HttpURLConnection.HTTP_BAD_REQUEST), new ApiExceptionInfo(ErrorCode.INVALID_PARAMETERS,
new InvalidParametersHandler()));
errorCodes.put(ApiURLs.LIST_CATEGORIES, categoryListApiCodes);
Map getCategoryApiCodes = new LinkedHashMap();
getCategoryApiCodes.put(String.valueOf(HttpURLConnection.HTTP_UNAUTHORIZED), new ApiExceptionInfo(ErrorCode.INVALID_SECRET_OR_COMPANY_ID));
getCategoryApiCodes.put(String.valueOf(HttpURLConnection.HTTP_BAD_REQUEST), new ApiExceptionInfo(ErrorCode.INVALID_PARAMETERS,
new InvalidParametersHandler()));
getCategoryApiCodes.put(String.valueOf(HttpURLConnection.HTTP_NOT_FOUND), new ApiExceptionInfo(ErrorCode.CATEGORY_NOT_FOUND));
errorCodes.put(ApiURLs.GET_CATEGORY, getCategoryApiCodes);
Map getMeteredData = new LinkedHashMap();
getMeteredData.put(String.valueOf(HttpURLConnection.HTTP_BAD_REQUEST), new ApiExceptionInfo(ErrorCode.INVALID_PARAMETERS,
new InvalidParametersHandler()));
errorCodes.put(ApiURLs.USER_VIEW_MOBILE, getMeteredData);
Map getProductsBought = new LinkedHashMap();
getProductsBought.put(String.valueOf(HttpURLConnection.HTTP_UNAUTHORIZED), new ApiExceptionInfo(ErrorCode.INVALID_SECRET_OR_COMPANY_ID));
getProductsBought.put(String.valueOf(HttpURLConnection.HTTP_BAD_REQUEST), new ApiExceptionInfo(ErrorCode.INVALID_PARAMETERS,
new InvalidParametersHandler()));
errorCodes.put(ApiURLs.USER_PRODUCTS, getProductsBought);
LOGGER.log(Level.FINEST, "Registered error codes: {0}", errorCodes);
}
/**
* Singleton instance retrieval method.
*
* @return Singleton instance of @{link {@link com.plenigo.sdk.internal.exceptions.ApiExceptionTranslator}
*/
public static ApiExceptionTranslator get() {
return INSTANCE;
}
/**
* Translates the API Response into a {@link PlenigoException} object.
*
* @param errorCode The error code that was received
* @param resource The resource that was called
* @param inputStream The response body
*
* @return The PlenigoException
*/
public PlenigoException translate(String errorCode, String resource, InputStream inputStream) {
Map exceptionInfoMap = errorCodes.get(resource);
if (exceptionInfoMap != null) {
ApiExceptionInfo exceptionInfo = exceptionInfoMap.get(errorCode);
if (exceptionInfo != null) {
LOGGER.log(Level.FINEST, "Found the error code information: {0}: ", exceptionInfo);
ErrorHandler handler = exceptionInfo.getHandler();
if (handler == null) {
handler = defaultHandler;
}
return handler.handle(exceptionInfo.getErrorCode(), resource, inputStream);
}
}
String defaultMessage = "Error code [" + errorCode + "] occured while querying the '" + resource + "' API URL, "
+ "response body: [" + SdkUtils.toString(inputStream) + "]";
LOGGER.log(Level.FINEST, "Error code {0} was not found, throwing PlenigoException with default message: {1}"
, new Object[]{errorCode, defaultMessage});
return new PlenigoException(errorCode, defaultMessage);
}
}