Skip to content

Instantly share code, notes, and snippets.

@gibson-khs
Last active November 28, 2017 08:29
Show Gist options
  • Select an option

  • Save gibson-khs/f929deb334460daf4527fa7b029e6f5d to your computer and use it in GitHub Desktop.

Select an option

Save gibson-khs/f929deb334460daf4527fa7b029e6f5d to your computer and use it in GitHub Desktop.
public class RestError {
@SerializedName("toast_message")
public String toast_message;
}
public static void setErrorToast(Context context, Throwable throwable) {
try {
if (throwable instanceof HttpException) {
HttpException exception = (HttpException) throwable;
// Api.retrofit == Retrofit object
RestError restError = (RestError) Api.retrofit.responseBodyConverter(
RestError.class, RestError.class.getAnnotations()).convert(exception.response().errorBody());
String errorMessage = restError.toast_message;
if (!TextUtils.isEmpty(errorMessage)) {
Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Toast.makeText(context, context.getResources().getString(R.string.default_error), Toast.LENGTH_LONG).show();
}
throwable.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment