com.foresee.api.sdk.exceptions.ApiExceptionsBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of public-api-sdk Show documentation
Show all versions of public-api-sdk Show documentation
SDK to access Foresee data over Public API
package com.foresee.api.sdk.exceptions;
import com.amazonaws.mobileconnectors.apigateway.ApiClientException;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import java.io.StringReader;
public final class ApiExceptionsBuilder {
/**
*
* @param ae The exception received on interacting with the client
* @return
*/
public static ApiException handleApiClientExceptions(ApiClientException ae) {
ApiException apiException;
try {
Gson gson = new Gson();
JsonReader reader = new JsonReader(new StringReader(ae.getMessage()));
reader.setLenient(true);
apiException = gson.fromJson(reader,ApiException.class);
/** */
if (apiException.getStatusCode() == 401) {
return handleInvalidAuthToken(apiException);
} else {
return apiException;
}
} catch (Exception e) {
return handleGenericException(e);
}
}
/**
* Any exceptions within the SDK will be captured here
* @param e The exception received on parse attempts
* @return
*/
public static ApiException handleGenericException(Exception e) {
return ApiException.builder().
message(e.getMessage()).
cause(e).
build();
}
public static InvalidTokenException handleInvalidAuthToken(ApiException ae) {
return new InvalidTokenException(ae.getStatusCode(), ae.getMessage(),
ae.getTimestamp(),ae.getDetailedErrors(), ae.getCause());
}
}