Last active
April 24, 2020 19:22
-
-
Save dmitriy-chernysh/dae104f913aa3dd836f280559511e447 to your computer and use it in GitHub Desktop.
OkHttp Interceptor to handle errors
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
/** | |
* Interceptor to handle connection errors and return a readable message to user | |
* <p> | |
* Created by Dmitriy Chernysh on 12/3/19. | |
* <p> | |
* https://instagr.am/mobiledevpro | |
* #MobileDevPro | |
* | |
* | |
* NOTE: add this to: | |
* | |
* OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); | |
* httpClient.interceptors().add(new HandleHttpErrorsInterceptor(context)); | |
* | |
* | |
*/ | |
public class HandleHttpErrorsInterceptor implements Interceptor { | |
private Context mAppContext; | |
public HandleHttpErrorsInterceptor(Context appContext) { | |
mAppContext = appContext; | |
} | |
@Override | |
public Response intercept(Chain chain) throws IOException { | |
try { | |
return chain.proceed(chain.request()); | |
} catch (ConnectException | SocketTimeoutException e) { | |
throw new IOException( | |
mAppContext.getResources().getString(R.string.message_check_internet_connection) | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment