Skip to content

Commit

Permalink
Merge pull request #2229 from wordpress-mobile/issue/2228-Fatal-Excep…
Browse files Browse the repository at this point in the history
…tion-java.lang.RuntimeException

Stats - Do not serialize VolleyErrors
  • Loading branch information
maxme committed Jan 21, 2015
2 parents be5215f + 91ac4e7 commit 3997a85
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.android.volley.VolleyError;

import org.wordpress.android.R;
import org.wordpress.android.ui.stats.exceptions.StatsError;
import org.wordpress.android.ui.stats.service.StatsService;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.DisplayUtils;
Expand Down Expand Up @@ -131,6 +132,19 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public void onSaveInstanceState(Bundle outState) {
//AppLog.d(AppLog.T.STATS, this.getTag() + " > saving instance state");

// Do not serialize VolleyError, but rewrite in a simple stats Exception.
// VolleyErrors should be serializable, but for some reason they are not.
// FIX for https://github.com/wordpress-mobile/WordPress-Android/issues/2228
if (mDatamodels != null) {
for (int i=0; i < mDatamodels.length; i++) {
if (mDatamodels[i] != null && mDatamodels[i] instanceof VolleyError) {
VolleyError currentVolleyError = (VolleyError) mDatamodels[i];
mDatamodels[i] = StatsUtils.rewriteVolleyError(currentVolleyError, getString(R.string.error_refresh_stats));
}
}
}

outState.putSerializable(ARG_REST_RESPONSE, mDatamodels);
super.onSaveInstanceState(outState);
}
Expand Down Expand Up @@ -258,7 +272,7 @@ protected boolean isErrorResponse() {

protected boolean isErrorResponse(int index) {
return mDatamodels != null && mDatamodels[index] != null
&& mDatamodels[index] instanceof VolleyError;
&& (mDatamodels[index] instanceof VolleyError || mDatamodels[index] instanceof StatsError);
}

protected boolean isSingleView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.WordPressDB;
import org.wordpress.android.models.Blog;
import org.wordpress.android.ui.stats.exceptions.StatsError;
import org.wordpress.android.ui.stats.models.AuthorsModel;
import org.wordpress.android.ui.stats.models.ClicksModel;
import org.wordpress.android.ui.stats.models.CommentFollowersModel;
Expand Down Expand Up @@ -337,4 +340,22 @@ public static synchronized Serializable parseResponse(StatsService.StatsEndpoint
return model;
}

/*
* This function rewrites a VolleyError into a simple Stats Error by getting the error message.
* This is a FIX for https://github.com/wordpress-mobile/WordPress-Android/issues/2228 where
* VolleyErrors cannot be serializable.
*/
public static StatsError rewriteVolleyError(VolleyError volleyError, String defaultErrorString) {
if (volleyError != null && volleyError.getMessage() != null) {
return new StatsError(volleyError.getMessage());
}

if (defaultErrorString != null) {
return new StatsError(defaultErrorString);
}

// Error string should be localized here, but don't want to pass a context
return new StatsError("Stats couldn't be refreshed at this time");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.ui.stats.exceptions.StatsError;
import org.wordpress.android.ui.stats.models.VisitModel;
import org.wordpress.android.ui.stats.models.VisitsModel;
import org.wordpress.android.ui.stats.service.StatsService;
Expand Down Expand Up @@ -166,6 +167,11 @@ public void onCreate(Bundle savedInstanceState) {
public void onSaveInstanceState(Bundle outState) {
//AppLog.d(T.STATS, "StatsVisitorsAndViewsFragment > saving instance state");

// FIX for https://github.com/wordpress-mobile/WordPress-Android/issues/2228
if (mVisitsData != null && mVisitsData instanceof VolleyError) {
VolleyError currentVolleyError = (VolleyError) mVisitsData;
mVisitsData = StatsUtils.rewriteVolleyError(currentVolleyError, getString(R.string.error_refresh_stats));
}
outState.putSerializable(ARG_REST_RESPONSE, mVisitsData);
outState.putInt(ARG_SELECTED_GRAPH_BAR, mSelectedBarGraphBarIndex);
outState.putInt(ARG_SELECTED_OVERVIEW_ITEM, mSelectedOverviewItemIndex);
Expand Down Expand Up @@ -214,7 +220,7 @@ private void updateUI() {
return;
}

if (mVisitsData instanceof VolleyError) {
if (mVisitsData instanceof VolleyError || mVisitsData instanceof StatsError) {
setupNoResultsUI(false);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.wordpress.android.ui.stats.exceptions;

import java.io.Serializable;

public class StatsError extends Exception implements Serializable {
public StatsError(String errorMessage) {
super(errorMessage);
}
}

0 comments on commit 3997a85

Please sign in to comment.