Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api error handling #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
package com.mindorks.framework.mvp.data.network;

import java.io.IOException;
import java.util.concurrent.Callable;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
Expand All @@ -37,44 +33,30 @@ public class ApiInterceptor implements Interceptor {

private static final String TAG = "ApiInterceptor";

private Observable<ApiHeader> mApiHeaderObservable;
private ApiHeader mApiHeader;

@Inject
public ApiInterceptor(final ApiHeader header) {
mApiHeaderObservable =
Observable.defer(new Callable<ObservableSource<? extends ApiHeader>>() {
@Override
public ObservableSource<? extends ApiHeader> call() throws Exception {
return Observable.just(header);
}
});
mApiHeader = header;
}

@Override
public Response intercept(Chain chain) throws IOException {
final Request request = chain.request();
final Request.Builder builder = request.newBuilder();
mApiHeaderObservable.subscribe(new Consumer<ApiHeader>() {
@Override
public void accept(ApiHeader header) throws Exception {

String apiAuthType = request.header(ApiHeader.API_AUTH_TYPE);
if (apiAuthType == null) {
apiAuthType = ApiHeader.PROTECTED_API;
}

switch (apiAuthType) {
case ApiHeader.PROTECTED_API:
builder.addHeader(ApiHeader.HEADER_PARAM_API_KEY, header.getApiKey());
builder.addHeader(ApiHeader.HEADER_PARAM_ACCESS_TOKEN, header.getAccessToken());
builder.addHeader(ApiHeader.HEADER_PARAM_USER_ID, String.valueOf(header.getUserId()));
break;
case ApiHeader.PUBLIC_API:
default:
builder.addHeader(ApiHeader.HEADER_PARAM_API_KEY, header.getApiKey());
}
}
});
String apiAuthType = request.header(ApiHeader.API_AUTH_TYPE);
if (apiAuthType == null) {
apiAuthType = ApiHeader.PROTECTED_API;
}

switch (apiAuthType) {
case ApiHeader.PROTECTED_API:
builder.addHeader(ApiHeader.HEADER_PARAM_ACCESS_TOKEN, mApiHeader.getAccessToken());
builder.addHeader(ApiHeader.HEADER_PARAM_USER_ID, String.valueOf(mApiHeader.getUserId()));
case ApiHeader.PUBLIC_API:
default:
builder.addHeader(ApiHeader.HEADER_PARAM_API_KEY, mApiHeader.getApiKey());
}

return chain.proceed(builder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.mindorks.framework.mvp.R;
import com.mindorks.framework.mvp.data.DataManager;
import com.mindorks.framework.mvp.data.network.model.ApiError;
import com.mindorks.framework.mvp.utils.AppConstants;

import java.io.IOException;
import java.net.HttpURLConnection;

import javax.inject.Inject;
import javax.net.ssl.HttpsURLConnection;

/**
* Base class that implements the Presenter interface and provides a base implementation for
Expand Down Expand Up @@ -72,27 +76,36 @@ public DataManager getDataManager() {
}

@Override
public void handleApiError(ApiError error) {

final GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
final Gson gson = builder.create();
public void handleApiError(Throwable throwable) {

ApiError apiError;
try {
if (error == null || error.getMessage() == null) {

if (!(throwable instanceof HttpException)) {
getMvpView().onError(R.string.api_default_error);
return;
}

HttpException httpException = (HttpException) throwable;

final GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
final Gson gson = builder.create();

ApiError apiError = gson.fromJson(httpException.response().errorBody().string(), ApiError.class);

if (apiError == null || apiError.getMessage() == null) {
getMvpView().onError(R.string.api_default_error);
return;
}
switch (error.getErrorCode()) {
case AppConstants.API_STATUS_CODE_BAD_REQUEST:
setUserAsLoggedOut();
switch (httpException.code()) {
case HttpsURLConnection.HTTP_UNAUTHORIZED:
getDataManager().setUserAsLoggedOut();
getMvpView().openActivityOnTokenExpire();
case AppConstants.API_STATUS_CODE_INTERNAL_SERVER_ERROR:
case AppConstants.API_STATUS_CODE_NOT_FOUND:
case HttpURLConnection.HTTP_INTERNAL_ERROR:
case HttpURLConnection.HTTP_NOT_FOUND:
default:
getMvpView().onError(error.getMessage());
getMvpView().onError(apiError.getMessage());
}
} catch (JsonSyntaxException | NullPointerException e) {
} catch (IOException | JsonSyntaxException | NullPointerException e) {
e.printStackTrace();
getMvpView().onError(R.string.api_default_error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
* Created by janisharali on 27/01/17.
*/

import com.mindorks.framework.mvp.data.network.model.ApiError;

/**
* Every presenter in the app must either implement this interface or extend BasePresenter
* indicating the MvpView type that wants to be attached with.
Expand All @@ -31,7 +29,7 @@ public interface MvpPresenter<V extends MvpView> {

void onDetach();

void handleApiError(ApiError error);
void handleApiError(Throwable throwable);

void setUserAsLoggedOut();
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void accept(LoginResponse response) throws Exception {
public void accept(Throwable throwable) throws Exception {
getMvpView().hideLoading();
// handle the login error here

// handleApiError(throwable);
//for demo the next screen in made to open even in failure
getMvpView().openMainActivity();
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public void accept(LoginResponse response) throws Exception {
public void accept(Throwable throwable) throws Exception {
getMvpView().hideLoading();
// handle the login error here

// handleApiError(throwable);
//for demo the next screen in made to open even in failure
getMvpView().openMainActivity();
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public void accept(LoginResponse response) throws Exception {
public void accept(Throwable throwable) throws Exception {
getMvpView().hideLoading();
// handle the login error here

// handleApiError(throwable);
//for demo the next screen in made to open even in failure
getMvpView().openMainActivity();
}
Expand Down