Add GraphQl error handling

This commit is contained in:
Ammar Githam 2021-03-30 20:44:01 +09:00
parent f7b918d91b
commit 07f41a8a8c
5 changed files with 39 additions and 21 deletions

View File

@ -142,8 +142,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
@Override @Override
protected void onCreate(@Nullable final Bundle savedInstanceState) { protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RetrofitFactory.setup(this); RetrofitFactory.setup(this);
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater()); binding = ActivityMainBinding.inflate(getLayoutInflater());
final String cookie = settingsHelper.getString(Constants.COOKIE); final String cookie = settingsHelper.getString(Constants.COOKIE);
CookieUtils.setupCookies(cookie); CookieUtils.setupCookies(cookie);
@ -253,7 +253,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
Log.e(TAG, "onDestroy: ", e); Log.e(TAG, "onDestroy: ", e);
} }
unbindActivityCheckerService(); unbindActivityCheckerService();
RetrofitFactory.getInstance().destroy(); RetrofitFactory.getInstance().destroy();
} }
@Override @Override
@ -853,4 +853,8 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
public Toolbar getToolbar() { public Toolbar getToolbar() {
return binding.toolbar; return binding.toolbar;
} }
public View getRootView() {
return binding.getRoot();
}
} }

View File

@ -13,15 +13,11 @@ import awais.instagrabber.repositories.NewsRepository;
import awais.instagrabber.repositories.responses.AymlResponse; import awais.instagrabber.repositories.responses.AymlResponse;
import awais.instagrabber.repositories.responses.AymlUser; import awais.instagrabber.repositories.responses.AymlUser;
import awais.instagrabber.repositories.responses.NewsInboxResponse; import awais.instagrabber.repositories.responses.NewsInboxResponse;
import awais.instagrabber.repositories.responses.Notification; import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.repositories.responses.NotificationArgs; import awais.instagrabber.repositories.responses.UserSearchResponse;
import awais.instagrabber.repositories.responses.NotificationCounts;
import awais.instagrabber.repositories.responses.NewsInboxResponse;
import awais.instagrabber.repositories.responses.notification.Notification; import awais.instagrabber.repositories.responses.notification.Notification;
import awais.instagrabber.repositories.responses.notification.NotificationArgs; import awais.instagrabber.repositories.responses.notification.NotificationArgs;
import awais.instagrabber.repositories.responses.notification.NotificationCounts; 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 awais.instagrabber.utils.Constants;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;

View File

@ -1,16 +1,10 @@
package awais.instagrabber.webservices; package awais.instagrabber.webservices;
import androidx.annotation.NonNull;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import awais.instagrabber.repositories.SearchRepository; import awais.instagrabber.repositories.SearchRepository;
import awais.instagrabber.repositories.responses.search.SearchResponse; import awais.instagrabber.repositories.responses.search.SearchResponse;
import awais.instagrabber.utils.TextUtils;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class SearchService extends BaseService { public class SearchService extends BaseService {
private static final String TAG = "LocationService"; private static final String TAG = "LocationService";
@ -20,10 +14,9 @@ public class SearchService extends BaseService {
private static SearchService instance; private static SearchService instance;
private SearchService() { private SearchService() {
final Retrofit retrofit = getRetrofitBuilder() repository = RetrofitFactory.getInstance()
.baseUrl("https://www.instagram.com") .getRetrofitWeb()
.build(); .create(SearchRepository.class);
repository = retrofit.create(SearchRepository.class);
} }
public static SearchService getInstance() { public static SearchService getInstance() {
@ -43,8 +36,8 @@ public class SearchService extends BaseService {
builder.put("context", context); builder.put("context", context);
builder.put("count", "50"); builder.put("count", "50");
return repository.search(isLoggedIn return repository.search(isLoggedIn
? "https://i.instagram.com/api/v1/fbsearch/topsearch_flat/" ? "https://i.instagram.com/api/v1/fbsearch/topsearch_flat/"
: "https://www.instagram.com/web/search/topsearch/", : "https://www.instagram.com/web/search/topsearch/",
builder.build()); builder.build());
} }
} }

View File

@ -1,10 +1,13 @@
package awais.instagrabber.webservices.interceptors; package awais.instagrabber.webservices.interceptors;
import android.util.Log; import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import com.google.android.material.snackbar.Snackbar;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
@ -51,6 +54,16 @@ public class IgErrorsInterceptor implements Interceptor {
// show dialog? // show dialog?
Log.e(TAG, "Network error: " + getMessage(errorCode, "The request start-line and/or headers are too large to process.")); Log.e(TAG, "Network error: " + getMessage(errorCode, "The request start-line and/or headers are too large to process."));
return; 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(); final ResponseBody body = response.body();
if (body == null) return; if (body == null) return;
@ -67,9 +80,13 @@ public class IgErrorsInterceptor implements Interceptor {
case "login_required": case "login_required":
showErrorDialog(R.string.login_required); showErrorDialog(R.string.login_required);
return; return;
case "execution failure":
showSnackbar(message);
return;
case "not authorized to view user": // Do we handle this in profile view fragment? 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 case "challenge_required": // Since we make users login using browser, we should not be getting this error in api requests
default: default:
showSnackbar(message);
Log.e(TAG, "checkError: " + bodyString); Log.e(TAG, "checkError: " + bodyString);
return; 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 @NonNull
private String getMessage(final int errorCode, final String message) { private String getMessage(final int errorCode, final String message) {
return String.format("code: %s, internalMessage: %s", errorCode, message); return String.format("code: %s, internalMessage: %s", errorCode, message);

View File

@ -481,4 +481,6 @@
<string name="inactive_user">User is inactive!</string> <string name="inactive_user">User is inactive!</string>
<string name="crash_report_subject">Barinsta Crash Report</string> <string name="crash_report_subject">Barinsta Crash Report</string>
<string name="crash_report_title">Select an email app to send crash logs</string> <string name="crash_report_title">Select an email app to send crash logs</string>
<string name="not_found">Not found!</string>
<string name="rate_limit">Your IP has been rate limited by Instagram. Wait for an hour and try again.</string>
</resources> </resources>