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

io.hypertrack.net.HyperTrackException Maven / Gradle / Ivy

The newest version!
package io.hypertrack.net;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Exception class for HyperTrack API error codes and messages.
 */
public class HyperTrackException extends RuntimeException {
    /** Status code and message of the error. */
    private int statusCode;
    private String message;

    /**
     * Instantiate exception from status code and message
     *
     * @param   message     Error message
     * @param   statusCode  Error code
     */
    public HyperTrackException(String message, int statusCode) {
        super(message);

        this.message = message;
        this.statusCode = statusCode;
    }

    public HyperTrackException(Throwable e) {
        this(null, e);
    }

    public HyperTrackException(String message, Throwable e) {
        super(message, e);
    }

    /**
     * Static method to parse response object into error.
     *
     *
     * @param   response    Response object
     * @return              Exception object
     * @throws  IOException
     */
    public static HyperTrackException parseResponse(HttpResponse response) throws IOException {
        try {
            int responseCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity, "UTF-8");

            return new HyperTrackException(responseString, responseCode);
        } catch (IOException e) {
            throw new IOException("Unable to decode response");
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy