Show specific reasons when posts fail to load.

This commit is contained in:
Docile-Alligator
2026-07-19 16:20:31 -04:00
parent 6b6f7bb019
commit 488ebf588b
6 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,7 @@
package ml.docilealligator.infinityforreddit
class RedditError(
val reason: String?,
val message: String?,
val error: Int?
)

View File

@ -364,6 +364,8 @@ public class HistoryPostFragment extends PostFragmentBase implements FragmentCom
if (e instanceof PostPagingSource.PostPagingSourceError) {
if (((PostPagingSource.PostPagingSourceError) e).code == 403 && Account.ANONYMOUS_ACCOUNT.equals(mActivity.accountName)) {
showErrorView(R.string.load_posts_error_anonymous_403);
} else if (((PostPagingSource.PostPagingSourceError) e).message != null) {
showErrorView(getString(R.string.load_posts_error_with_reason, ((PostPagingSource.PostPagingSourceError) e).message));
} else {
showErrorView(R.string.load_posts_error);
}
@ -466,6 +468,16 @@ public class HistoryPostFragment extends PostFragmentBase implements FragmentCom
}
}
@Override
protected void showErrorView(String errorMessage) {
if (mActivity != null && isAdded()) {
binding.swipeRefreshLayoutHistoryPostFragment.setRefreshing(false);
binding.fetchPostInfoLinearLayoutHistoryPostFragment.setVisibility(View.VISIBLE);
binding.fetchPostInfoTextViewHistoryPostFragment.setText(errorMessage);
mGlide.load(R.drawable.error_image).into(binding.fetchPostInfoImageViewHistoryPostFragment);
}
}
@NonNull
@Override
protected SwipeRefreshLayout getSwipeRefreshLayout() {

View File

@ -964,6 +964,8 @@ public class PostFragment extends PostFragmentBase implements FragmentCommunicat
if (e instanceof PostPagingSource.PostPagingSourceError) {
if (((PostPagingSource.PostPagingSourceError) e).code == 403 && Account.ANONYMOUS_ACCOUNT.equals(mActivity.accountName)) {
showErrorView(R.string.load_posts_error_anonymous_403);
} else if (((PostPagingSource.PostPagingSourceError) e).message != null) {
showErrorView(getString(R.string.load_posts_error_with_reason, ((PostPagingSource.PostPagingSourceError) e).message));
} else {
showErrorView(R.string.load_posts_error);
}
@ -1164,6 +1166,16 @@ public class PostFragment extends PostFragmentBase implements FragmentCommunicat
}
}
@Override
protected void showErrorView(String errorMessage) {
if (mActivity != null && isAdded()) {
binding.swipeRefreshLayoutPostFragment.setRefreshing(false);
binding.fetchPostInfoLinearLayoutPostFragment.setVisibility(View.VISIBLE);
binding.fetchPostInfoTextViewPostFragment.setText(errorMessage);
mGlide.load(R.drawable.error_image).into(binding.fetchPostInfoImageViewPostFragment);
}
}
@NonNull
@Override
protected SwipeRefreshLayout getSwipeRefreshLayout() {

View File

@ -592,6 +592,8 @@ public abstract class PostFragmentBase extends Fragment {
protected abstract void showErrorView(int stringResId);
protected abstract void showErrorView(String errorMessage);
@NonNull
protected abstract SwipeRefreshLayout getSwipeRefreshLayout();

View File

@ -9,6 +9,7 @@ import androidx.paging.PagingState;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
@ -21,6 +22,7 @@ import java.util.concurrent.Executor;
import java.util.stream.Collectors;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.RedditError;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.postfilter.PostFilter;
@ -30,6 +32,7 @@ import ml.docilealligator.infinityforreddit.readpost.ReadPostsListInterface;
import ml.docilealligator.infinityforreddit.thing.SortType;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import okhttp3.ResponseBody;
import retrofit2.HttpException;
import retrofit2.Response;
import retrofit2.Retrofit;
@ -264,7 +267,16 @@ public class PostPagingSource extends ListenableFuturePagingSource<String, Post>
}
}
} else {
return new LoadResult.Error<>(new PostPagingSourceError(response.code(), "Error getting response"));
//{"reason": "banned", "message": "Not Found", "error": 404}
try (ResponseBody errorBody = response.errorBody()) {
if (errorBody != null) {
RedditError redditError = new Gson().fromJson(errorBody.string(), RedditError.class);
return new LoadResult.Error<>(new PostPagingSourceError(response.code(), redditError.getReason()));
}
} catch (IOException e) {
e.printStackTrace();
}
return new LoadResult.Error<>(new PostPagingSourceError(response.code(), null));
}
}

View File

@ -120,6 +120,7 @@
<string name="load_posts_error">Error loading posts.\nTap to retry.</string>
<string name="load_posts_error_anonymous_403">Due to platform limitations, content cannot be loaded right now.
\n\nYou can try again shortly or subscribe for a more consistent experience.</string>
<string name="load_posts_error_with_reason">Error loading posts. Reason: %1$s.\nTap to retry.</string>
<string name="load_more_posts_error">Error loading posts.</string>
<string name="load_post_error">Error loading this post.\nTap to retry.</string>
<string name="search_subreddits_error">Error searching subreddits.\nTap to retry.</string>