Commit 79d73a1c authored by Bartek Fabiszewski's avatar Bartek Fabiszewski
Browse files

Improve error reporting

parent 58faffbf
Loading
Loading
Loading
Loading
+11 −9
Original line number Diff line number Diff line
@@ -75,13 +75,15 @@ class WebHelper {
    private static final String PARAM_TRACK = "track";

    private final String userAgent;
    private final Context context;


    /**
     * Constructor
     * @param context Context
     * @param ctx Context
     */
    WebHelper(Context context) {
    WebHelper(Context ctx) {
        context = ctx;
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        user = prefs.getString("prefUsername", "NULL");
        pass = prefs.getString("prefPass", "NULL");
@@ -145,7 +147,7 @@ class WebHelper {
                    String location = connection.getHeaderField("Location");
                    if (Logger.DEBUG) { Log.d(TAG, "[postWithParams redirect: " + location + "]"); }
                    if (location == null || redirectTries == 0) {
                        throw new IOException("Illegal redirect: " + responseCode);
                        throw new IOException(context.getString(R.string.e_illegal_redirect, responseCode));
                    }
                    redirect = true;
                    redirectTries--;
@@ -153,14 +155,14 @@ class WebHelper {
                    String h1 = base.getHost();
                    String h2 = url.getHost();
                    if (h1 != null && !h1.equalsIgnoreCase(h2)) {
                        throw new IOException("Illegal redirect: " + responseCode);
                        throw new IOException(context.getString(R.string.e_illegal_redirect, responseCode));
                    }
                }
                else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
                    throw new WebAuthException("Authorization failure: " + responseCode);
                    throw new WebAuthException(context.getString(R.string.e_auth_failure, responseCode));
                }
                else if (responseCode != HttpURLConnection.HTTP_OK) {
                    throw new IOException("HTTP error code: " + responseCode);
                    throw new IOException(context.getString(R.string.e_http_code, responseCode));
                }
            } while (redirect);

@@ -200,7 +202,7 @@ class WebHelper {
            if (Logger.DEBUG) { Log.d(TAG, "[postPosition json failed: " + e +"]"); }
        }
        if (error) {
            throw new IOException("response error");
            throw new IOException(context.getString(R.string.e_server_response));
        }
    }

@@ -221,7 +223,7 @@ class WebHelper {
            JSONObject json = new JSONObject(response);
            boolean error = json.getBoolean("error");
            if (error) {
                throw new IOException("Server error");
                throw new IOException(context.getString(R.string.e_server_response));
            } else {
                return json.getInt("trackid");
            }
@@ -247,7 +249,7 @@ class WebHelper {
        JSONObject json = new JSONObject(response);
        boolean error = json.getBoolean("error");
        if (error) {
            throw new WebAuthException("Server error");
            throw new WebAuthException(context.getString(R.string.e_server_response));
        }
    }

+25 −7
Original line number Diff line number Diff line
@@ -20,6 +20,11 @@ import android.util.Log;
import org.json.JSONException;

import java.io.IOException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.NoRouteToHostException;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@@ -75,7 +80,7 @@ public class WebSyncService extends IntentService {
            try {
                web.authorize();
            } catch (WebAuthException|IOException|JSONException e) {
                handleError(e.getMessage());
                handleError(e);
                return;
            }

@@ -105,12 +110,12 @@ public class WebSyncService extends IntentService {
            } catch (IOException e) {
                if (Logger.DEBUG) { Log.d(TAG, "[websync io exception: " + e + "]"); }
                // schedule retry
                handleError(e.getMessage());
                handleError(e);
            } catch (WebAuthException e) {
                if (Logger.DEBUG) { Log.d(TAG, "[websync auth exception: " + e + "]"); }
                isAuthorized = false;
                // schedule retry
                handleError(e.getMessage());
                handleError(e);
            }
        }
        return trackId;
@@ -124,6 +129,8 @@ public class WebSyncService extends IntentService {
    private void doSync(int trackId) {
        // iterate over positions in db
        Cursor cursor = db.getUnsynced();
        // suppress as it requires target api 19
        //noinspection TryFinallyCanBeTryWithResources
        try {
            while (cursor.moveToNext()) {
                int rowId = cursor.getInt(cursor.getColumnIndex(DbContract.Positions._ID));
@@ -138,12 +145,12 @@ public class WebSyncService extends IntentService {
            // handle web errors
            if (Logger.DEBUG) { Log.d(TAG, "[websync io exception: " + e + "]"); }
            // schedule retry
            handleError(e.getMessage());
            handleError(e);
        } catch (WebAuthException e) {
            if (Logger.DEBUG) { Log.d(TAG, "[websync auth exception: " + e + "]"); }
            isAuthorized = false;
            // schedule retry
            handleError(e.getMessage());
            handleError(e);
        } finally {
            cursor.close();
        }
@@ -153,10 +160,21 @@ public class WebSyncService extends IntentService {
     * Actions performed in case of synchronization error.
     * Send broadcast to main activity, schedule retry if tracking is on.
     *
     * @param message Error message
     * @param e Exception
     */
    private void handleError(String message) {
    private void handleError(Exception e) {
        String message;
        if (e instanceof UnknownHostException) {
            message = getString(R.string.e_unknown_host, e.getMessage());
        } else if (e instanceof MalformedURLException || e instanceof URISyntaxException) {
            message = getString(R.string.e_bad_url, e.getMessage());
        } else if (e instanceof ConnectException || e instanceof NoRouteToHostException) {
            message = getString(R.string.e_connect, e.getMessage());
        } else {
            message = e.getMessage();
        }
        if (Logger.DEBUG) { Log.d(TAG, "[websync retry: " + message + "]"); }

        db.setError(message);
        Intent intent = new Intent(BROADCAST_SYNC_FAILED);
        intent.putExtra("message", message);
+7 −0
Original line number Diff line number Diff line
@@ -88,6 +88,13 @@
    <string name="unit_mile">mi.</string>
    <string name="provide_valid_url">Please provide valid server url</string>
    <string name="provide_user_pass_url">Please enter user, password and server url first</string>
    <string name="e_illegal_redirect">Illegal redirect: %d</string>
    <string name="e_auth_failure">Authorization failure: %d</string>
    <string name="e_http_code">HTTP error code: %d</string>
    <string name="e_server_response">Server response error</string>
    <string name="e_unknown_host">Unknown host: %s</string>
    <string name="e_bad_url">Bad URL: %s</string>
    <string name="e_connect">Connection error: %s</string>
    <plurals name="label_positions_behind">
        <item quantity="one">%d position behind</item>
        <item quantity="other">%d positions behind</item>