Show readable error string

This commit is contained in:
seven332 committed 2015-05-16 14:11:28 +08:00
1 parent 0489ef2c39
commit 7b21001af0
6 files changed
+107 -20

No files matched your search

@@ -26,14 +26,16 @@ import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.hippo.effect.ViewTransition;
import com.hippo.ehviewer.R;
import com.hippo.util.AssertUtils;
import com.hippo.util.ExceptionUtils;
import com.hippo.util.IntIdGenerator;
import com.hippo.util.LayoutManagerUtils;
import com.hippo.util.Log;
import com.hippo.util.UiUtils;
import com.hippo.widget.Snackbar;
import com.hippo.widget.recyclerview.EasyRecyclerView;
import com.hippo.widget.refreshlayout.RefreshLayout;
@@ -48,7 +50,6 @@ public class ContentLayout extends FrameLayout {
private EasyRecyclerView mRecyclerView;
private View mImageView;
private TextView mTextView;
private Snackbar mSnackbar;
private ContentHelper mContentHelper;
@@ -77,7 +78,6 @@ public class ContentLayout extends FrameLayout {
mProgressBar = (ProgressBar) getChildAt(0);
mTipView = (ViewGroup) getChildAt(1);
mRefreshLayout = (RefreshLayout) getChildAt(2);
mSnackbar = (Snackbar) getChildAt(3);
mRecyclerView = (EasyRecyclerView) mRefreshLayout.getChildAt(1);
mImageView = mTipView.getChildAt(0);
mTextView = (TextView) mTipView.getChildAt(1);
@@ -98,9 +98,6 @@ public class ContentLayout extends FrameLayout {
//
mRecyclerViewOriginTop = mRecyclerView.getPaddingTop();
mRecyclerViewOriginBottom = mRecyclerView.getPaddingBottom();
// Snackbar
mSnackbarOriginBottom = mSnackbar.getPaddingBottom();
}
public EasyRecyclerView getRecyclerView() {
@@ -124,8 +121,6 @@ public class ContentLayout extends FrameLayout {
mRecyclerView.setPadding(mRecyclerView.getPaddingLeft(),
mRecyclerView.getPaddingTop(), mRecyclerView.getPaddingRight(),
mRecyclerViewOriginBottom + fitPaddingBottom);
// Snackbar
mSnackbar.setPadding(mSnackbar.getPaddingLeft(), mSnackbar.getPaddingTop(), mSnackbar.getPaddingRight(), mSnackbarOriginBottom + fitPaddingBottom);
}
@Override
@@ -163,7 +158,6 @@ public class ContentLayout extends FrameLayout {
private EasyRecyclerView mRecyclerView;
private View mImageView;
private TextView mTextView;
private Snackbar mSnackbar;
private RecyclerView.LayoutManager mLayoutManager;
private ViewTransition mViewTransition;
@@ -275,7 +269,6 @@ public class ContentLayout extends FrameLayout {
mProgressBar = contentLayout.mProgressBar;
mTipView = contentLayout.mTipView;
mRefreshLayout = contentLayout.mRefreshLayout;
mSnackbar = contentLayout.mSnackbar;
mRecyclerView = contentLayout.mRecyclerView;
mImageView = contentLayout.mImageView;
mTextView = contentLayout.mTextView;
@@ -291,7 +284,6 @@ public class ContentLayout extends FrameLayout {
mRefreshLayout.setOnFooterRefreshListener(this);
mTipView.setOnClickListener(this);
mSnackbar.setAction(mContext.getString(R.string.retry), this);
}
protected abstract RecyclerView.LayoutManager generateLayoutManager();
@@ -441,7 +433,17 @@ public class ContentLayout extends FrameLayout {
if (mCurrentTaskId == taskId) {
mRefreshLayout.setHeaderRefreshing(false);
mRefreshLayout.setFooterRefreshing(false);
showText(e.getClass().getName());
Log.d("Get page data failed " + e.getClass().getName() + " " + e.getMessage());
String readableError = ExceptionUtils.getReadableString(mContext, e);
String reason = ExceptionUtils.getReasonString(mContext, e);
if (reason != null) {
readableError += '\n' + reason;
}
if (mViewTransition.getShownViewIndex() == 0) {
Toast.makeText(mContext, readableError, Toast.LENGTH_SHORT).show();
} else {
showText(readableError);
}
}
}
@@ -65,6 +65,14 @@ public class ResponseCodeException extends Exception {
mMessage = message;
}
public int getResponseCode() {
return mResponseCode;
}
public boolean isIdentifiedResponseCode() {
return !DEFAULT_ERROR_MESSAGE.equals(mMessage);
}
@Override
public String getMessage() {
return mMessage;
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.util;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.hippo.ehviewer.R;
import com.hippo.network.RedirectionException;
import com.hippo.network.ResponseCodeException;
import org.apache.http.conn.ConnectTimeoutException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
public final class ExceptionUtils {
public static @NonNull String getReadableString(Context context, Exception e) {
if (e instanceof ConnectTimeoutException ||
e instanceof SocketTimeoutException) {
return context.getString(R.string.em_timeout);
} else if (e instanceof UnknownHostException) {
return context.getString(R.string.em_unknown_host);
} else if (e instanceof ResponseCodeException) {
ResponseCodeException responseCodeException = (ResponseCodeException) e;
String error = String.format(context.getString(R.string.em_response_code),
responseCodeException.getResponseCode());
if (responseCodeException.isIdentifiedResponseCode()) {
error += '\n' + responseCodeException.getMessage();
}
return error;
} else if (e instanceof RedirectionException) {
return context.getString(R.string.em_redirection);
} else if (e instanceof SocketException) {
return context.getString(R.string.em_socket);
} else {
Log.d("Can't recognize this Exception", e);
return context.getString(R.string.em_unknown);
}
}
public static @Nullable String getReasonString(Context context, Exception e) {
if (e instanceof UnknownHostException) {
return context.getString(R.string.erm_unknown_host);
} else {
return null;
}
}
}