Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Created November 1, 2017 17:01
Show Gist options
  • Save afiqiqmal/f97599f7574af27054fb051c63a154f0 to your computer and use it in GitHub Desktop.
Save afiqiqmal/f97599f7574af27054fb051c63a154f0 to your computer and use it in GitHub Desktop.
This is a sample class utils for handling errors message
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.crashlytics.android.Crashlytics;
import com.google.firebase.crash.FirebaseCrash;
import com.google.gson.Gson;
import com.iteractive.bepunct.R;
import com.iteractive.bepunct.client.Constant;
import com.iteractive.bepunct.client.entity.response.ErrorResponse;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import okhttp3.ResponseBody;
import retrofit2.HttpException;
import retrofit2.Response;
/**
* @author : hafiq on 23/01/2017.
*/
@Singleton
public class ErrorUtils {
private Context mContext;
private boolean LOG = false;
private Throwable throwable;
@Inject
public ErrorUtils(Context context){
this.mContext = context;
}
public void checkError(Throwable e){
try {
throwable = e;
//fireBaseReportError(e);
//Crashlytics.logException(e);
e.printStackTrace();
if (e instanceof HttpException) {
try {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
assert responseBody != null;
//making my own ErrorResponse if the error returning from API.. but make sure standardize
ErrorResponse response = new Gson().fromJson(responseBody.toString(), ErrorResponse.class);
httpMessage(response.getMessage());
} catch (Exception ignored) {
//catch normal error shows
int code = getHttpErrorCode(e);
httpMessage(code);
}
}
else if (e instanceof ConnectException) {
showToast("Slow Internet Connection. Please Try Again");
} else if (e instanceof UnknownHostException || e instanceof SocketTimeoutException) {
showToast("Make Sure Your Internet Connection is Properly");
} else if (e instanceof SSLHandshakeException || e instanceof SSLPeerUnverifiedException) {
showToast("Server Connection Problem. Please Try Again");
} else {
showToast("Opps.. Something went wrong…");
}
}
catch (Exception err){
err.printStackTrace();
}
}
private int getHttpErrorCode(Throwable e){
Response body = ((HttpException) e).response();
return body.code();
}
private void httpMessage(String custom) {
showToast(custom);
}
//only common http error
private void httpMessage(int code){
if (code == 400){
showToast("Bad Request");
}
else if (code == 401) {
showToast("Not Authorized Access");
}
else if (code == 403) {
showToast("Forbidden Access");
}
else if (code == 404) {
showToast("Request Not Found");
}
else if (code == 405) {
showToast("Request Not Allowed");
}
else if (code == 407){
showToast("Proxy Authentication Required");
}
else if (code == 408){
showToast("Data Request Expired");
}
else if (code == 500) {
showToast("Internal Server Error Occurred");
}
else if (code == 502){
showToast("Bad Url Gateway");
}
else if (code == 503){
showToast("Service is Unavailable. Please Try Again");
}
else{
showErrorLog("Error code : "+code);
}
}
private void showToast(String message){
if (!LOG) {
if (mContext != null) {
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
}
}
showErrorLog(message+": "+throwable.getMessage());
}
private void showErrorLog(String message){
Log.e(Constant.LOGTAG,message);
}
private void fireBaseReportError(Throwable e){
//FirebaseCrash.report(e);
}
public void recordError(Throwable e){
try {
//extra errorlytics
//fireBaseReportError(e);
//Crashlytics.logException(e);
e.printStackTrace();
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
protected ErrorUtils errorUtils;
public class ContactActivity {
....
....
....
@Override
public void showError(Throwable throwable) {
errorUtils.checkError(throwable);
}
...
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment