package es.sandwatch.httprequests;

import android.util.Log;

import com.android.volley.NetworkError;
import com.android.volley.NetworkResponse;
import com.android.volley.NoConnectionError;
import com.android.volley.ServerError;
import com.android.volley.VolleyError;


/**
 * Created by isma on 12/25/15.
 */
public class HttpRequestError{
    private static final String TAG = "HttpRequestError";

    private static final int ERROR_TYPE_SERVER = 1;
    private static final int ERROR_TYPE_NETWORK = 2;
    private static final int ERROR_TYPE_OTHER = 3;

    private final int mErrorType;
    private final String mMessage;
    private final int mStatusCode;


    HttpRequestError(VolleyError error){
        Log.d(TAG, error.toString());
        NetworkResponse response = error.networkResponse;
        if (error instanceof ServerError && response != null && response.data != null){
            mErrorType = ERROR_TYPE_SERVER;
            mMessage = new String(response.data);
            mStatusCode = response.statusCode;
            Log.d(TAG, "Server error: " + response.statusCode);
        }
        else if (error instanceof NoConnectionError || error instanceof NetworkError){
            mErrorType = ERROR_TYPE_NETWORK;
            mMessage = "Offline, check your internet connection";
            mStatusCode = -1;
        }
        else{
            mErrorType = ERROR_TYPE_OTHER;
            mMessage = error.getMessage();
            mStatusCode = -1;
        }
    }

    public boolean isServerError(){
        return mErrorType == ERROR_TYPE_SERVER;
    }

    public boolean isNetworkError(){
        return mErrorType == ERROR_TYPE_NETWORK;
    }

    public String getMessage(){
        return mMessage;
    }

    public int returnStatusCode(){
        return mStatusCode;
    }
}

@@ -275,35 +275,104 @@
  * C
-reated by isma on 12/25/15.
+ontains all the information about a request error.%0A *%0A * @author Ismael Alonso%0A * @version 1.0.0
 %0A */
@@ -725,16 +725,158 @@
 Code;%0A%0A%0A
+    /**%0A     * Constructor. Extracts the relevant information from a VolleyError.%0A     *%0A     * @param error the source volley error.%0A     */%0A
     Http
@@ -1700,251 +1700,821 @@
     
-public boolean isServerError()%7B%0A        return mErrorType == ERROR_TYPE_SERVER;%0A    %7D%0A%0A    public boolean isNetworkError()%7B%0A        return mErrorType == ERROR_TYPE_NETWORK;%0A    %7D%0A%0A    public String getMessage()%7B%0A        return mMessage;%0A    %7D%0A
+/**%0A     * Determines whether this error is a server error.%0A     *%0A     * @return true if it is a server error, false otherwise.%0A     */%0A    public boolean isServerError()%7B%0A        return mErrorType == ERROR_TYPE_SERVER;%0A    %7D%0A%0A    /**%0A     * Determines whether this error is a network error.%0A     *%0A     * @return true if this is a network error, false otherwise.%0A     */%0A    public boolean isNetworkError()%7B%0A        return mErrorType == ERROR_TYPE_NETWORK;%0A    %7D%0A%0A    /**%0A     * Gets the message of this error.%0A     *%0A     * @return the message of this error.%0A     */%0A    public String getMessage()%7B%0A        return mMessage;%0A    %7D%0A%0A    /**%0A     * Gets the HTTP status code if this is a server error.%0A     *%0A     * @return the HTTP status code of the server error if this is a server error, -1 otherwise.%0A     */
 %0A   

Document HttpRequestError