package com.proxerme.library.connection;

import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;

import com.afollestad.bridge.BridgeException;
import com.proxerme.library.connection.ProxerException.ErrorCode;

import org.json.JSONException;

import static com.proxerme.library.connection.ProxerException.ERROR_IO;
import static com.proxerme.library.connection.ProxerException.ERROR_NETWORK;
import static com.proxerme.library.connection.ProxerException.ERROR_PROXER;
import static com.proxerme.library.connection.ProxerException.ERROR_TIMEOUT;
import static com.proxerme.library.connection.ProxerException.ERROR_UNKNOWN;
import static com.proxerme.library.connection.ProxerException.ERROR_UNPARSEABLE;

/**
 * A Helper class, which converts an Exception to a Integer, represented through the
 * {@link ErrorCode} annotation. It also has
 * a method to convert an ErrorCode into a human readable String.
 *
 * @author Ruben Gees
 */
class ProxerErrorHandler {

    /**
     * Handles a JSONException and returns the appropriate {@link ProxerException}.
     *
     * @param jsonException The Exception.
     * @return The appropriate {@link ProxerException}.
     */
    @SuppressWarnings("UnusedParameters")
    @NonNull
    @CheckResult
    public static ProxerException handleException(@NonNull JSONException jsonException) {
        return new ProxerException(ERROR_UNPARSEABLE);
    }

    /**
     * Handles a BridgeException and returns the appropriate {@link ProxerException}.
     *
     * @param bridgeException The Exception.
     * @return The appropriate {@link ProxerException}.
     */
    @NonNull
    @CheckResult
    public static ProxerException handleException(@NonNull BridgeException bridgeException) {
        ProxerException exception;

        switch (bridgeException.reason()) {
            case BridgeException.REASON_REQUEST_TIMEOUT: {
                exception = new ProxerException(ERROR_TIMEOUT);

                break;
            }

            case BridgeException.REASON_RESPONSE_UNSUCCESSFUL: {
                exception = new ProxerException(ERROR_NETWORK);

                break;
            }

            case BridgeException.REASON_RESPONSE_UNPARSEABLE: {
                exception = new ProxerException(ERROR_UNPARSEABLE);

                break;
            }

            case BridgeException.REASON_RESPONSE_IOERROR: {
                exception = new ProxerException(ERROR_IO);

                break;
            }

            case BridgeException.REASON_RESPONSE_VALIDATOR_FALSE: {
                exception = new ProxerException(ERROR_UNKNOWN);

                break;
            }

            case BridgeException.REASON_RESPONSE_VALIDATOR_ERROR: {
                exception = new ProxerException(ERROR_PROXER,
                        bridgeException.getMessage());

                break;
            }

            case BridgeException.REASON_REQUEST_CANCELLED: {
                exception = new ProxerException(ERROR_UNKNOWN);

                break;
            }

            case BridgeException.REASON_REQUEST_FAILED: {
                exception = new ProxerException(ERROR_NETWORK);

                break;
            }

            default: {
                exception = new ProxerException(ERROR_UNKNOWN);

                break;
            }
        }

        return exception;
    }

}

@@ -333,22 +333,16 @@
 ception.
-ERROR_
 IO;%0Aimpo
@@ -399,22 +399,16 @@
 ception.
-ERROR_
 NETWORK;
@@ -474,90 +474,8 @@
 ion.
-ERROR_PROXER;%0Aimport static com.proxerme.library.connection.ProxerException.ERROR_
 TIME
@@ -541,22 +541,16 @@
 ception.
-ERROR_
 UNKNOWN;
@@ -612,22 +612,16 @@
 ception.
-ERROR_
 UNPARSEA
@@ -1157,39 +1157,32 @@
 CheckResult%0A    
-public 
 static ProxerExc
@@ -1271,38 +1271,32 @@
 ProxerException(
-ERROR_
 UNPARSEABLE);%0A  
@@ -1548,15 +1548,8 @@
     
-public 
 stat
@@ -1814,22 +1814,16 @@
 ception(
-ERROR_
 TIMEOUT)
@@ -1968,38 +1968,32 @@
 ProxerException(
-ERROR_
 NETWORK);%0A%0A     
@@ -2137,22 +2137,16 @@
 ception(
-ERROR_
 UNPARSEA
@@ -2298,22 +2298,16 @@
 ception(
-ERROR_
 IO);%0A%0A  
@@ -2450,38 +2450,32 @@
 ProxerException(
-ERROR_
 UNKNOWN);%0A%0A     
@@ -2595,36 +2595,33 @@
     exception = 
-new 
+(
 ProxerException(
@@ -2623,75 +2623,46 @@
 tion
-(ERROR_PROXER,%0A                        bridgeException.getMessage()
+) bridgeException.underlyingException(
 );%0A%0A
@@ -2800,38 +2800,32 @@
 ProxerException(
-ERROR_
 UNKNOWN);%0A%0A     
@@ -2963,22 +2963,16 @@
 ception(
-ERROR_
 NETWORK)
@@ -3087,14 +3087,8 @@
 ion(
-ERROR_
 UNKN
@@ -3134,32 +3134,74 @@
    %7D%0A        %7D%0A%0A
+        //noinspection ConstantConditions%0A
         return e

Fix ErrorHandler