All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foresee.api.sdk.exceptions.ApiExceptionsBuilder Maven / Gradle / Ivy

There is a newer version: 1.2.4-M3
Show newest version
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());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy