Last active
November 28, 2017 08:29
-
-
Save gibson-khs/f929deb334460daf4527fa7b029e6f5d to your computer and use it in GitHub Desktop.
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 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