From 07f41a8a8c524e4009ea1be8f7537f86573b6f80 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Tue, 30 Mar 2021 20:44:01 +0900 Subject: [PATCH] Add GraphQl error handling --- .../instagrabber/activities/MainActivity.java | 8 +++++-- .../instagrabber/webservices/NewsService.java | 8 ++----- .../webservices/SearchService.java | 19 +++++---------- .../interceptors/IgErrorsInterceptor.java | 23 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 ++ 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/activities/MainActivity.java b/app/src/main/java/awais/instagrabber/activities/MainActivity.java index 2da9cb24..95c801ef 100644 --- a/app/src/main/java/awais/instagrabber/activities/MainActivity.java +++ b/app/src/main/java/awais/instagrabber/activities/MainActivity.java @@ -142,8 +142,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage @Override protected void onCreate(@Nullable final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); RetrofitFactory.setup(this); + super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); final String cookie = settingsHelper.getString(Constants.COOKIE); CookieUtils.setupCookies(cookie); @@ -253,7 +253,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage Log.e(TAG, "onDestroy: ", e); } unbindActivityCheckerService(); - RetrofitFactory.getInstance().destroy(); + RetrofitFactory.getInstance().destroy(); } @Override @@ -853,4 +853,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage public Toolbar getToolbar() { return binding.toolbar; } + + public View getRootView() { + return binding.getRoot(); + } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/webservices/NewsService.java b/app/src/main/java/awais/instagrabber/webservices/NewsService.java index a7664a8a..15b34def 100644 --- a/app/src/main/java/awais/instagrabber/webservices/NewsService.java +++ b/app/src/main/java/awais/instagrabber/webservices/NewsService.java @@ -13,15 +13,11 @@ import awais.instagrabber.repositories.NewsRepository; import awais.instagrabber.repositories.responses.AymlResponse; import awais.instagrabber.repositories.responses.AymlUser; import awais.instagrabber.repositories.responses.NewsInboxResponse; -import awais.instagrabber.repositories.responses.Notification; -import awais.instagrabber.repositories.responses.NotificationArgs; -import awais.instagrabber.repositories.responses.NotificationCounts; -import awais.instagrabber.repositories.responses.NewsInboxResponse; +import awais.instagrabber.repositories.responses.User; +import awais.instagrabber.repositories.responses.UserSearchResponse; import awais.instagrabber.repositories.responses.notification.Notification; import awais.instagrabber.repositories.responses.notification.NotificationArgs; import awais.instagrabber.repositories.responses.notification.NotificationCounts; -import awais.instagrabber.repositories.responses.User; -import awais.instagrabber.repositories.responses.UserSearchResponse; import awais.instagrabber.utils.Constants; import retrofit2.Call; import retrofit2.Callback; diff --git a/app/src/main/java/awais/instagrabber/webservices/SearchService.java b/app/src/main/java/awais/instagrabber/webservices/SearchService.java index 39b5bd65..4144f85d 100644 --- a/app/src/main/java/awais/instagrabber/webservices/SearchService.java +++ b/app/src/main/java/awais/instagrabber/webservices/SearchService.java @@ -1,16 +1,10 @@ package awais.instagrabber.webservices; -import androidx.annotation.NonNull; - import com.google.common.collect.ImmutableMap; import awais.instagrabber.repositories.SearchRepository; import awais.instagrabber.repositories.responses.search.SearchResponse; -import awais.instagrabber.utils.TextUtils; import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; public class SearchService extends BaseService { private static final String TAG = "LocationService"; @@ -20,10 +14,9 @@ public class SearchService extends BaseService { private static SearchService instance; private SearchService() { - final Retrofit retrofit = getRetrofitBuilder() - .baseUrl("https://www.instagram.com") - .build(); - repository = retrofit.create(SearchRepository.class); + repository = RetrofitFactory.getInstance() + .getRetrofitWeb() + .create(SearchRepository.class); } public static SearchService getInstance() { @@ -43,8 +36,8 @@ public class SearchService extends BaseService { builder.put("context", context); builder.put("count", "50"); return repository.search(isLoggedIn - ? "https://i.instagram.com/api/v1/fbsearch/topsearch_flat/" - : "https://www.instagram.com/web/search/topsearch/", - builder.build()); + ? "https://i.instagram.com/api/v1/fbsearch/topsearch_flat/" + : "https://www.instagram.com/web/search/topsearch/", + builder.build()); } } diff --git a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java index 7d424a2d..e27eeae2 100644 --- a/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java +++ b/app/src/main/java/awais/instagrabber/webservices/interceptors/IgErrorsInterceptor.java @@ -1,10 +1,13 @@ package awais.instagrabber.webservices.interceptors; import android.util.Log; +import android.view.View; import androidx.annotation.NonNull; import androidx.annotation.StringRes; +import com.google.android.material.snackbar.Snackbar; + import org.json.JSONObject; import java.io.IOException; @@ -51,6 +54,16 @@ public class IgErrorsInterceptor implements Interceptor { // show dialog? Log.e(TAG, "Network error: " + getMessage(errorCode, "The request start-line and/or headers are too large to process.")); return; + case 404: + showErrorDialog(R.string.not_found); + return; + case 302: // redirect + final String location = response.header("location"); + if (location.equals("https://www.instagram.com/accounts/login/")) { + // rate limited + showErrorDialog(R.string.rate_limit); + } + return; } final ResponseBody body = response.body(); if (body == null) return; @@ -67,9 +80,13 @@ public class IgErrorsInterceptor implements Interceptor { case "login_required": showErrorDialog(R.string.login_required); return; + case "execution failure": + showSnackbar(message); + return; case "not authorized to view user": // Do we handle this in profile view fragment? case "challenge_required": // Since we make users login using browser, we should not be getting this error in api requests default: + showSnackbar(message); Log.e(TAG, "checkError: " + bodyString); return; } @@ -88,6 +105,12 @@ public class IgErrorsInterceptor implements Interceptor { } } + private void showSnackbar(final String message) { + final View view = mainActivity.getRootView(); + if (view == null) return; + Snackbar.make(view, message, Snackbar.LENGTH_LONG).show(); + } + @NonNull private String getMessage(final int errorCode, final String message) { return String.format("code: %s, internalMessage: %s", errorCode, message); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ce22f77f..86b2d1ac 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -481,4 +481,6 @@ User is inactive! Barinsta Crash Report Select an email app to send crash logs + Not found! + Your IP has been rate limited by Instagram. Wait for an hour and try again.