Skip to content

Instantly share code, notes, and snippets.

@iblinov65apps
Created March 6, 2018 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iblinov65apps/534e079d084b8d719d8d71a122e8e6aa to your computer and use it in GitHub Desktop.
Save iblinov65apps/534e079d084b8d719d8d71a122e8e6aa to your computer and use it in GitHub Desktop.
package ru.alexfitness.presentation.core.utils;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import com.google.gson.JsonParseException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;
import javax.inject.Inject;
import ru.alexfitness.R;
import ru.alexfitness.data.api.ApiException;
import ru.alexfitness.data.api.HttpCode;
import ru.alexfitness.data.api.IllegalLoginPasswordError;
import ru.alexfitness.data.api.NetworkConnectionException;
import ru.alexfitness.presentation.core.view.ErrorView;
class ErrorHandlerCreatorImpl implements ErrorHandlerCreator {
@Inject
ErrorHandlerCreatorImpl() {
}
@Override
public Builder newBuilder(ErrorView errorView, Throwable throwable) {
return new ErrorHandlerBuilderImpl(errorView, throwable);
}
@Override
public Builder defaultBuilder(ErrorView errorView, Throwable throwable) {
return new ErrorHandlerBuilderImpl(errorView, throwable)
.checkNetworkErrors()
.checkApiExceptions()
.defaultError(R.string.error_unknown);
}
static class ErrorHandlerImpl implements ErrorHandler {
private final ErrorHandlerBuilderImpl builder;
ErrorHandlerImpl(ErrorHandlerBuilderImpl builder) {
this.builder = builder;
}
@Override
public void handleErrors() {
ErrorView errorView = builder.errorView;
Throwable throwable = builder.throwable;
throwable.printStackTrace();
boolean isHandled = false;
// network errors
if (builder.checkNetworkErrors) {
isHandled = checkNetworkErrors(errorView, throwable);
}
// api exceptions
if (!isHandled & builder.checkApiExceptions) {
isHandled = checkApiExceptions(errorView, throwable);
}
// another throwable
if (!isHandled & builder.onAnotherThrowableListener != null) {
isHandled = builder.onAnotherThrowableListener.handleThrowable(throwable);
}
// default error
if (!isHandled & builder.defaultErrorRes != null) {
errorView.showErrorDialog(builder.defaultErrorRes);
}
// call anyway if set
if (builder.onAnyThrowableListener != null) {
builder.onAnyThrowableListener.handleThrowable(throwable);
}
// ignore others (only log)
}
private boolean checkNetworkErrors(ErrorView errorView, Throwable throwable) {
if (throwable instanceof NetworkConnectionException) {
errorView.showNoConnectionFragment();
return true;
}
return false;
}
private boolean checkApiExceptions(ErrorView errorView, Throwable throwable) {
if (throwable instanceof ApiException) {
// error from server
return handleApiException(errorView, ((ApiException) throwable));
} else if (throwable instanceof IllegalLoginPasswordError) {
// illegal login/password error
errorView.showAuthFragment();
return true;
} else if (isConnectionError(throwable)) {
// server connection unable error
errorView.showErrorDialog(R.string.error_on_server_connection);
return true;
} else if (isParseException(throwable)) {
// server connection unable error
errorView.showErrorDialog(R.string.error_server);
return true;
}
return false;
}
private boolean handleApiException(ErrorView errorView, ApiException exception) {
String description = exception.getDescription();
if (description != null) {
errorView.showErrorDialog(description);
return true;
}
int httpCode = exception.getHttpCode();
switch (httpCode) {
case HttpCode.NOT_ACCEPTABLE_406:
errorView.showErrorDialog(R.string.error_server);
return true;
case HttpCode.BAD_REQUEST_400:
errorView.showErrorDialog(R.string.error_on_get_data_from_server);
return true;
default:
OnApiCodeExceptionListener listener = builder.onApiCodeExceptionListener;
if (listener != null) {
listener.onApiCodeException(httpCode);
return true;
}
break;
}
return false;
}
private boolean isConnectionError(Throwable throwable) {
return throwable instanceof ConnectException
|| throwable instanceof TimeoutException
|| throwable instanceof SocketException
|| throwable instanceof SocketTimeoutException;
}
private boolean isParseException(Throwable throwable) {
return throwable instanceof JsonParseException;
}
}
static class ErrorHandlerBuilderImpl implements Builder {
private final ErrorView errorView;
private final Throwable throwable;
private boolean checkNetworkErrors = false;
private boolean checkApiExceptions = false;
@Nullable
private OnApiCodeExceptionListener onApiCodeExceptionListener;
@Nullable
private OnAnotherThrowableListener onAnotherThrowableListener;
@Nullable
private OnAnyThrowableListener onAnyThrowableListener;
@Nullable
@StringRes
private Integer defaultErrorRes;
ErrorHandlerBuilderImpl(ErrorView errorView, Throwable throwable) {
this.errorView = errorView;
this.throwable = throwable;
}
@Override
public Builder checkNetworkErrors() {
checkNetworkErrors = true;
return this;
}
@Override
public Builder checkApiExceptions() {
checkApiExceptions = true;
return this;
}
@Override
public Builder apiCodeExceptionListener(OnApiCodeExceptionListener listener) {
this.onApiCodeExceptionListener = listener;
return this;
}
@Override
public Builder anotherThrowableListener(OnAnotherThrowableListener listener) {
this.onAnotherThrowableListener = listener;
return this;
}
@Override
public Builder anyThrowableListener(OnAnyThrowableListener listener) {
this.onAnyThrowableListener = listener;
return this;
}
@Override
public Builder defaultError(@StringRes int defaultErrorRes) {
this.defaultErrorRes = defaultErrorRes;
return this;
}
@Override
public ErrorHandler build() {
return new ErrorHandlerImpl(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment