Created
June 2, 2014 11:36
-
-
Save JMPergar/e9447962f857684d5585 to your computer and use it in GitHub Desktop.
Base Callback to centralize the management of errors if you use Appsly Android REST.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class BaseApiCallback<Result> extends Callback<Result> { | |
private final static String CLASS_NAME = "BaseApiCallback"; | |
@Override | |
public final void onResponse(Response<Result> response) { | |
if (response.getStatusCode() == 0) { | |
LogManager.error(CLASS_NAME, "onResponse", response.getError().getMessage()); | |
notifyError(ErrorType.ERROR_CONECTION); | |
return; | |
} | |
if (isChildOfBaseResponse(response.getResult())) { | |
manangerResponse((Response<BaseApiResponse>) response); | |
} else { | |
throw new IllegalArgumentException("response must be extends BaseApiResponse"); | |
} | |
} | |
private void manangerResponse(Response<BaseApiResponse> response) { | |
BaseApiResponse result = response.getResult(); | |
if (ErrorManager.isValidResponse(response.getStatusCode(), result)) { | |
LogManager.verbose(CLASS_NAME, "managerResponse", "Response success"); | |
notifyDataLoaded(result); | |
} else { | |
ErrorType error = ErrorManager.managerError(response); | |
notifyError(error); | |
} | |
} | |
private boolean isChildOfBaseResponse(Result instance) { | |
return BaseApiResponse.class.isAssignableFrom(instance.getClass()); | |
} | |
public abstract void notifyDataLoaded(BaseApiResponse response); | |
public abstract void notifyError(ErrorType error); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BaseApiResponse { | |
private String message; | |
private String code; | |
public String getMessage() { | |
return message; | |
} | |
public void setMessage(String message) { | |
this.message = message; | |
} | |
public String getCode() { | |
return code; | |
} | |
public void setCode(String code) { | |
this.code = code; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ErrorManager { | |
private final static String CLASS_NAME = "ErrorManager"; | |
public static boolean isValidResponse(int statusCode, BaseApiResponse response) { | |
boolean res = statusCode == HttpStatus.SC_OK | |
&& response != null | |
&& response.getCode() == null; | |
return res; | |
} | |
public static ErrorType managerError(Response<BaseApiResponse> response) { | |
if (response.getStatusCode() != HttpStatus.SC_OK) { | |
return managerHttpError(response.getStatusCode(), response.getError().getMessage()); | |
} else { | |
return managerApiError(response.getResult()); | |
} | |
} | |
public static ErrorType managerApiError(BaseApiResponse response) { | |
LogManager.error(CLASS_NAME, "managerHttpError", response.getCode() + " " + response.getMessage()); | |
return ErrorType.ERROR_SERVER; | |
} | |
public static ErrorType managerHttpError(int statusCode, String message) { | |
LogManager.error(CLASS_NAME, "managerHttpError", statusCode + " " + message); | |
ErrorType error; | |
switch (statusCode) { | |
case HttpStatus.SC_FORBIDDEN: | |
error = ErrorType.ERROR_CONECTION; | |
break; | |
case HttpStatus.SC_GONE: | |
error = ErrorType.ERROR_CONECTION; | |
break; | |
case HttpStatus.SC_NOT_FOUND: | |
error = ErrorType.ERROR_CONECTION; | |
break; | |
case HttpStatus.SC_INTERNAL_SERVER_ERROR: | |
error = ErrorType.ERROR_SERVER; | |
break; | |
default: | |
error = ErrorType.ERROR_CONECTION; | |
break; | |
} | |
return error; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum ErrorType { | |
ERROR_UNDEFINED, | |
ERROR_CONECTION, | |
ERROR_SERVER, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment