Report last error at end of sync

pull/14/head
Sam Bosley 14 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) { protected void handleException(String tag, Exception e, boolean displayError) {
//TODO: When Crittercism supports it, report error to them //TODO: When Crittercism supports it, report error to them
final Context context = ContextManager.getContext(); final Context context = ContextManager.getContext();
getUtilities().setLastError(e.toString()); getUtilities().setLastError(e.toString(), "");
String message = null; String message = null;

@ -3,6 +3,7 @@ package com.todoroo.astrid.sync;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.text.TextUtils;
import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.DateUtilities; 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 = "_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$ protected static final String PREF_ONGOING = "_ongoing"; //$NON-NLS-1$
/** Get preferences object from the context */ /** Get preferences object from the context */
@ -83,6 +86,11 @@ abstract public class SyncProviderUtilities {
return getPrefs().getString(getIdentifier() + PREF_LAST_ERROR, null); 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 */ /** @return Last Error, or null if no last error */
public boolean isOngoing() { public boolean isOngoing() {
return getPrefs().getBoolean(getIdentifier() + PREF_ONGOING, false); return getPrefs().getBoolean(getIdentifier() + PREF_ONGOING, false);
@ -96,9 +104,10 @@ abstract public class SyncProviderUtilities {
} }
/** Set Last Error */ /** Set Last Error */
public void setLastError(String error) { public void setLastError(String error, String type) {
Editor editor = getPrefs().edit(); Editor editor = getPrefs().edit();
editor.putString(getIdentifier() + PREF_LAST_ERROR, error); editor.putString(getIdentifier() + PREF_LAST_ERROR, error);
editor.putString(getIdentifier() + PREF_LAST_ERROR_TYPE, type);
editor.commit(); editor.commit();
} }
@ -117,12 +126,26 @@ abstract public class SyncProviderUtilities {
editor.commit(); 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 */ /** Set Last Attempted Sync Date */
public void recordSyncStart() { public void recordSyncStart() {
Editor editor = getPrefs().edit(); Editor editor = getPrefs().edit();
editor.putLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC, editor.putLong(getIdentifier() + PREF_LAST_ATTEMPTED_SYNC,
DateUtilities.now()); DateUtilities.now());
editor.remove(getIdentifier() + PREF_LAST_ERROR); editor.remove(getIdentifier() + PREF_LAST_ERROR);
editor.remove(getIdentifier() + PREF_LAST_ERROR_TYPE);
editor.putBoolean(getIdentifier() + PREF_ONGOING, true); editor.putBoolean(getIdentifier() + PREF_ONGOING, true);
editor.commit(); editor.commit();
} }

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

@ -9,6 +9,8 @@ import com.timsu.astrid.R;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.RemoteModel;
import com.todoroo.astrid.data.Update; 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.sync.SyncProviderUtilities;
import com.todoroo.astrid.utility.AstridPreferences; import com.todoroo.astrid.utility.AstridPreferences;
@ -72,6 +74,11 @@ public class ActFmPreferenceService extends SyncProviderUtilities {
private static JSONObject user = null; 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 * Return JSON object user, either yourself or the user of the model
* @param update * @param update

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

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

@ -2,6 +2,8 @@ package com.todoroo.astrid.gtasks;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.service.StatisticsConstants;
import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.sync.SyncProviderUtilities; import com.todoroo.astrid.sync.SyncProviderUtilities;
/** /**
@ -49,4 +51,9 @@ public class GtasksPreferenceService extends SyncProviderUtilities {
return Preferences.getStringValue(PREF_USER_NAME); 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 java.io.IOException;
import android.text.TextUtils;
public class GoogleTasksException extends IOException { public class GoogleTasksException extends IOException {
private static final long serialVersionUID = -5585448790574862510L; private static final long serialVersionUID = -5585448790574862510L;
public GoogleTasksException(String message) { private String type;
public GoogleTasksException(String message, String type) {
super(message); super(message);
this.type = type;
} }
public GoogleTasksException(String message, Throwable cause) { public GoogleTasksException(String message, Throwable cause) {
@ -18,4 +23,10 @@ public class GoogleTasksException extends IOException {
super(cause.getMessage()); super(cause.getMessage());
initCause(cause); 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.GoogleTasksException;
import com.todoroo.astrid.gtasks.api.GtasksInvoker; import com.todoroo.astrid.gtasks.api.GtasksInvoker;
@SuppressWarnings("nls")
public class GtasksTokenValidator { public class GtasksTokenValidator {
private static final String TOKEN_INTENT_RECEIVED = "intent!"; //$NON-NLS-1$ 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); String accountName = Preferences.getStringValue(GtasksPreferenceService.PREF_USER_NAME);
Account a = accountManager.getAccountByName(accountName); Account a = accountManager.getAccountByName(accountName);
if (a == null) { 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++) { 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) { private static boolean testToken(String token) {
@ -77,7 +78,7 @@ public class GtasksTokenValidator {
if(result == null) if(result == null)
throw new NullPointerException("Future result was null."); //$NON-NLS-1$ throw new NullPointerException("Future result was null."); //$NON-NLS-1$
} catch (Exception e) { } catch (Exception e) {
throw new GoogleTasksException(e.getLocalizedMessage()); throw new GoogleTasksException(e.getLocalizedMessage(), "future-error");
} }
// check what kind of result was returned // check what kind of result was returned
@ -89,10 +90,10 @@ public class GtasksTokenValidator {
if (c instanceof Activity) if (c instanceof Activity)
c.startActivity(intent); c.startActivity(intent);
else 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; return TOKEN_INTENT_RECEIVED;
} else { } 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; return token;
} }

@ -47,7 +47,6 @@ import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.service.TaskService; import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.sync.SyncResultCallback; import com.todoroo.astrid.sync.SyncResultCallback;
import com.todoroo.astrid.sync.SyncV2Provider; import com.todoroo.astrid.sync.SyncV2Provider;
import com.todoroo.astrid.sync.SyncV2Provider.SyncExceptionHandler;
public class GtasksSyncV2Provider extends SyncV2Provider { public class GtasksSyncV2Provider extends SyncV2Provider {
@ -113,8 +112,10 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
final GtasksInvoker invoker = new GtasksInvoker(authToken); final GtasksInvoker invoker = new GtasksInvoker(authToken);
try { try {
gtasksListService.updateLists(invoker.allGtaskLists()); gtasksListService.updateLists(invoker.allGtaskLists());
} catch (GoogleTasksException e) {
handler.handleException("gtasks-sync=io", e, e.getType()); //$NON-NLS-1$
} catch (IOException e) { } 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(); StoreObject[] lists = gtasksListService.getLists();
@ -130,9 +131,7 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
synchronizeListHelper(list, invoker, manual, handler, callback); synchronizeListHelper(list, invoker, manual, handler, callback);
callback.incrementProgress(25); callback.incrementProgress(25);
if (finisher.decrementAndGet() == 0) { if (finisher.decrementAndGet() == 0) {
gtasksPreferenceService.recordSuccessfulSync(); finishSync(callback);
gtasksPreferenceService.stopOngoing();
callback.finished();
} }
} }
}).start(); }).start();
@ -156,8 +155,10 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
task.readFromCursor(queued); task.readFromCursor(queued);
try { try {
gtasksSyncService.pushTaskOnSave(task, task.getMergedValues(), invoker, false); gtasksSyncService.pushTaskOnSave(task, task.getMergedValues(), invoker, false);
} catch (GoogleTasksException e) {
handler.handleException("gtasks-sync-io", e, e.getType()); //$NON-NLS-1$
} catch (IOException e) { } catch (IOException e) {
handler.handleException("gtasks-sync-io", e); //$NON-NLS-1$ handler.handleException("gtasks-sync-io", e, e.getMessage()); //$NON-NLS-1$
} finally { } finally {
callback.incrementProgress(10); callback.incrementProgress(10);
} }
@ -256,9 +257,12 @@ public class GtasksSyncV2Provider extends SyncV2Provider {
gtasksTaskListUpdater.correctOrderAndIndentForList(listId); gtasksTaskListUpdater.correctOrderAndIndentForList(listId);
} }
} catch (GoogleTasksException e) {
if (errorHandler != null)
errorHandler.handleException("gtasks-sync-io", e, e.getType()); //$NON-NLS-1$
} catch (IOException e) { } catch (IOException e) {
if (errorHandler != null) 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())) if(intent != null && SYNC_ACTION.equals(intent.getAction()))
startSynchronization(this); startSynchronization(this);
} catch (Exception e) { } 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_FILTER = "task-completed-filter";
public static final String TASK_COMPLETED_NOTIFICATION = "task-completed-notification"; 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_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_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 PDV_SYNC_FINISHED = "pdv-sync-finished";
public static final String TASK_CREATED_TASKLIST = "task-created-tasklist"; public static final String TASK_CREATED_TASKLIST = "task-created-tasklist";
public static final String ACTFM_LOGIN_SHOW = "actfm-login-show"; public static final String ACTFM_LOGIN_SHOW = "actfm-login-show";

Loading…
Cancel
Save