Report last error at end of sync

pull/14/head
Sam Bosley 12 years ago
parent 1576702202
commit 3f0de0ab4e

@ -379,7 +379,7 @@ public abstract class SyncProvider<TYPE extends SyncContainer> {
protected void handleException(String tag, Exception e, boolean displayError) {
//TODO: When Crittercism supports it, report error to them
final Context context = ContextManager.getContext();
getUtilities().setLastError(e.toString());
getUtilities().setLastError(e.toString(), "");
String message = null;

@ -3,6 +3,7 @@ package com.todoroo.astrid.sync;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.DateUtilities;
@ -32,6 +33,8 @@ abstract public class SyncProviderUtilities {
protected static final String PREF_LAST_ERROR = "_last_err"; //$NON-NLS-1$
protected static final String PREF_LAST_ERROR_TYPE = "_last_err_type"; //$NON-NLS-1$
protected static final String PREF_ONGOING = "_ongoing"; //$NON-NLS-1$
/** Get preferences object from the context */
@ -83,6 +86,11 @@ abstract public class SyncProviderUtilities {
return getPrefs().getString(getIdentifier() + PREF_LAST_ERROR, null);
}
/** @return Last Error type, or null if no last error */
public String getLastErrorType() {
return getPrefs().getString(getIdentifier() + PREF_LAST_ERROR_TYPE, null);
}
/** @return Last Error, or null if no last error */
public boolean isOngoing() {
return getPrefs().getBoolean(getIdentifier() + PREF_ONGOING, false);
@ -96,9 +104,10 @@ abstract public class SyncProviderUtilities {
}
/** Set Last Error */
public void setLastError(String error) {
public void setLastError(String error, String type) {
Editor editor = getPrefs().edit();
editor.putString(getIdentifier() + PREF_LAST_ERROR, error);
editor.putString(getIdentifier() + PREF_LAST_ERROR_TYPE, type);
editor.commit();
}
@ -117,12 +126,26 @@ abstract public class SyncProviderUtilities {
editor.commit();
}
/** Report last error if one exists */
public void reportLastError() {
String lastError = getLastError();
if (!TextUtils.isEmpty(lastError)) {
String type = getLastErrorType();
reportLastErrorImpl(lastError, type);
}
}
protected void reportLastErrorImpl(String lastError, String type) {
// Subclasses can override if necessary
}
/** Set Last Attempted Sync Date */
public void recordSyncStart() {
Editor editor = getPrefs().edit();
editor.putLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC,
DateUtilities.now());
editor.remove(getIdentifier() + PREF_LAST_ERROR);
editor.remove(getIdentifier() + PREF_LAST_ERROR_TYPE);
editor.putBoolean(getIdentifier() + PREF_ONGOING, true);
editor.commit();
}

@ -10,9 +10,9 @@ import com.todoroo.andlib.service.ExceptionService;
abstract public class SyncV2Provider {
public class SyncExceptionHandler {
public void handleException(String tag, Exception e) {
public void handleException(String tag, Exception e, String type) {
//TODO: When Crittercism supports it, report error to them
getUtilities().setLastError(e.toString());
getUtilities().setLastError(e.toString(), type);
// occurs when application was closed
if(e instanceof IllegalStateException) {
@ -76,6 +76,14 @@ abstract public class SyncV2Provider {
*/
abstract protected SyncProviderUtilities getUtilities();
protected void finishSync(SyncResultCallback callback) {
SyncProviderUtilities utilities = getUtilities();
utilities.recordSuccessfulSync();
utilities.reportLastError();
utilities.stopOngoing();
callback.finished();
}
@Override
public String toString() {
return getName();

@ -9,6 +9,8 @@ import com.timsu.astrid.R;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.Update;
import com.todoroo.astrid.service.StatisticsConstants;
import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.sync.SyncProviderUtilities;
import com.todoroo.astrid.utility.AstridPreferences;
@ -72,6 +74,11 @@ public class ActFmPreferenceService extends SyncProviderUtilities {
private static JSONObject user = null;
@Override
protected void reportLastErrorImpl(String lastError, String type) {
StatisticsService.reportEvent(StatisticsConstants.ACTFM_SYNC_ERROR, "type", type); //$NON-NLS-1$
}
/**
* Return JSON object user, either yourself or the user of the model
* @param update

@ -1099,7 +1099,7 @@ public final class ActFmSyncService {
} catch (IOException e) {
if (handler != null)
handler.handleException("io-exception-list-" + model, e);
handler.handleException("io-exception-list-" + model, e, e.getMessage());
else
handleException("io-exception-list-" + model, e);
} catch (JSONException e) {

@ -140,15 +140,13 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
time = actFmSyncService.fetchTags(time);
Preferences.setInt(LAST_TAG_FETCH_TIME, time);
} catch (JSONException e) {
handler.handleException("actfm-sync", e); //$NON-NLS-1$
handler.handleException("actfm-sync", e, e.getMessage()); //$NON-NLS-1$
} catch (IOException e) {
handler.handleException("actfm-sync", e); //$NON-NLS-1$
handler.handleException("actfm-sync", e, e.getMessage()); //$NON-NLS-1$
} finally {
callback.incrementProgress(20);
if(finisher.decrementAndGet() == 0) {
actFmPreferenceService.recordSuccessfulSync();
actFmPreferenceService.stopOngoing();
callback.finished();
finishSync(callback);
}
}
}
@ -165,9 +163,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
callback.incrementProgress(30);
if(finisher.decrementAndGet() == 0) {
actFmPreferenceService.recordSuccessfulSync();
actFmPreferenceService.stopOngoing();
callback.finished();
finishSync(callback);
}
}
});
@ -196,9 +192,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
} finally {
callback.incrementProgress(20);
if(finisher.decrementAndGet() == 0) {
actFmPreferenceService.recordSuccessfulSync();
actFmPreferenceService.stopOngoing();
callback.finished();
finishSync(callback);
}
}
}

@ -2,6 +2,8 @@ package com.todoroo.astrid.gtasks;
import com.timsu.astrid.R;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.service.StatisticsConstants;
import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.sync.SyncProviderUtilities;
/**
@ -49,4 +51,9 @@ public class GtasksPreferenceService extends SyncProviderUtilities {
return Preferences.getStringValue(PREF_USER_NAME);
}
@Override
protected void reportLastErrorImpl(String lastError, String type) {
StatisticsService.reportEvent(StatisticsConstants.GTASKS_SYNC_ERROR, "type", type); //$NON-NLS-1$
}
}

@ -2,11 +2,16 @@ package com.todoroo.astrid.gtasks.api;
import java.io.IOException;
import android.text.TextUtils;
public class GoogleTasksException extends IOException {
private static final long serialVersionUID = -5585448790574862510L;
public GoogleTasksException(String message) {
private String type;
public GoogleTasksException(String message, String type) {
super(message);
this.type = type;
}
public GoogleTasksException(String message, Throwable cause) {
@ -18,4 +23,10 @@ public class GoogleTasksException extends IOException {
super(cause.getMessage());
initCause(cause);
}
public String getType() {
if (!TextUtils.isEmpty(type))
return type;
return getMessage();
}
}

@ -18,6 +18,7 @@ import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.api.GoogleTasksException;
import com.todoroo.astrid.gtasks.api.GtasksInvoker;
@SuppressWarnings("nls")
public class GtasksTokenValidator {
private static final String TOKEN_INTENT_RECEIVED = "intent!"; //$NON-NLS-1$
@ -39,7 +40,7 @@ public class GtasksTokenValidator {
String accountName = Preferences.getStringValue(GtasksPreferenceService.PREF_USER_NAME);
Account a = accountManager.getAccountByName(accountName);
if (a == null) {
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountNotFound, accountName));
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountNotFound, accountName), "account-not-found");
}
for (int i = 0; i < REVALIDATION_TRIES; i++) {
@ -56,7 +57,7 @@ public class GtasksTokenValidator {
}
throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh));
throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh), "auth-token-refresh");
}
private static boolean testToken(String token) {
@ -77,7 +78,7 @@ public class GtasksTokenValidator {
if(result == null)
throw new NullPointerException("Future result was null."); //$NON-NLS-1$
} catch (Exception e) {
throw new GoogleTasksException(e.getLocalizedMessage());
throw new GoogleTasksException(e.getLocalizedMessage(), "future-error");
}
// check what kind of result was returned
@ -89,10 +90,10 @@ public class GtasksTokenValidator {
if (c instanceof Activity)
c.startActivity(intent);
else
throw new GoogleTasksException(c.getString(R.string.gtasks_error_background_sync_auth));
throw new GoogleTasksException(c.getString(R.string.gtasks_error_background_sync_auth), "background-auth-error");
return TOKEN_INTENT_RECEIVED;
} else {
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager));
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager), "account-manager-error");
}
return token;
}

@ -47,7 +47,6 @@ import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.sync.SyncResultCallback;
import com.todoroo.astrid.sync.SyncV2Provider;
import com.todoroo.astrid.sync.SyncV2Provider.SyncExceptionHandler;
public class GtasksSyncV2Provider extends SyncV2Provider {
@ -113,8 +112,10 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
final GtasksInvoker invoker = new GtasksInvoker(authToken);
try {
gtasksListService.updateLists(invoker.allGtaskLists());
} catch (GoogleTasksException e) {
handler.handleException("gtasks-sync=io", e, e.getType()); //$NON-NLS-1$
} catch (IOException e) {
handler.handleException("gtasks-sync=io", e); //$NON-NLS-1$
handler.handleException("gtasks-sync=io", e, e.getMessage()); //$NON-NLS-1$
}
StoreObject[] lists = gtasksListService.getLists();
@ -130,9 +131,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
synchronizeListHelper(list, invoker, manual, handler, callback);
callback.incrementProgress(25);
if (finisher.decrementAndGet() == 0) {
gtasksPreferenceService.recordSuccessfulSync();
gtasksPreferenceService.stopOngoing();
callback.finished();
finishSync(callback);
}
}
}).start();
@ -156,8 +155,10 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
task.readFromCursor(queued);
try {
gtasksSyncService.pushTaskOnSave(task, task.getMergedValues(), invoker, false);
} catch (GoogleTasksException e) {
handler.handleException("gtasks-sync-io", e, e.getType()); //$NON-NLS-1$
} catch (IOException e) {
handler.handleException("gtasks-sync-io", e); //$NON-NLS-1$
handler.handleException("gtasks-sync-io", e, e.getMessage()); //$NON-NLS-1$
} finally {
callback.incrementProgress(10);
}
@ -256,9 +257,12 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
gtasksTaskListUpdater.correctOrderAndIndentForList(listId);
}
} catch (GoogleTasksException e) {
if (errorHandler != null)
errorHandler.handleException("gtasks-sync-io", e, e.getType()); //$NON-NLS-1$
} catch (IOException e) {
if (errorHandler != null)
errorHandler.handleException("gtasks-sync-io", e); //$NON-NLS-1$
errorHandler.handleException("gtasks-sync-io", e, e.getMessage()); //$NON-NLS-1$
}
}

@ -38,7 +38,7 @@ public class MilkBackgroundService extends Service {
if(intent != null && SYNC_ACTION.equals(intent.getAction()))
startSynchronization(this);
} catch (Exception e) {
MilkUtilities.INSTANCE.setLastError(e.toString());
MilkUtilities.INSTANCE.setLastError(e.toString(), "");
}
}

@ -41,7 +41,9 @@ public class StatisticsConstants {
public static final String TASK_COMPLETED_FILTER = "task-completed-filter";
public static final String TASK_COMPLETED_NOTIFICATION = "task-completed-notification";
public static final String ACTFM_SYNC_FINISHED = "actfm-sync-finished";
public static final String ACTFM_SYNC_ERROR = "actfm-sync-error";
public static final String GTASKS_SYNC_FINISHED = "gtasks-sync-finished";
public static final String GTASKS_SYNC_ERROR = "gtasks-sync-error";
public static final String PDV_SYNC_FINISHED = "pdv-sync-finished";
public static final String TASK_CREATED_TASKLIST = "task-created-tasklist";
public static final String ACTFM_LOGIN_SHOW = "actfm-login-show";

Loading…
Cancel
Save