-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TIQR-460: handle no connection (#164)
Add interceptor Small cleanup from review Co-authored-by: Iulia STANA <[email protected]>
- Loading branch information
1 parent
91faa2d
commit 2fd109c
Showing
5 changed files
with
67 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
40 changes: 40 additions & 0 deletions
40
app/src/main/kotlin/nl/eduid/network/ConnectivityInterceptor.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package nl.eduid.network | ||
|
||
import android.net.TrafficStats | ||
import okhttp3.Interceptor | ||
import okhttp3.Protocol | ||
import okhttp3.Response | ||
import okhttp3.ResponseBody.Companion.toResponseBody | ||
import timber.log.Timber | ||
import java.net.HttpURLConnection | ||
import java.net.SocketTimeoutException | ||
import java.net.UnknownHostException | ||
|
||
class ConnectivityInterceptor : Interceptor { | ||
private companion object { | ||
const val NO_INTERNET = "No internet connection" | ||
} | ||
|
||
override fun intercept(chain: Interceptor.Chain): Response { | ||
TrafficStats.setThreadStatsTag(Thread.currentThread().id.toInt()) | ||
return try { | ||
chain.proceed(chain.request()) | ||
} catch (e: UnknownHostException) { | ||
processException(e, chain) | ||
} catch (e: SocketTimeoutException) { | ||
processException(e, chain) | ||
} | ||
} | ||
|
||
private fun processException(e: Exception, chain: Interceptor.Chain): Response { | ||
Timber.d("$NO_INTERNET: exception ${e.message}", e) | ||
return Response | ||
.Builder() | ||
.code(HttpURLConnection.HTTP_CLIENT_TIMEOUT) | ||
.message(NO_INTERNET) | ||
.request(chain.request()) | ||
.protocol(Protocol.HTTP_1_1) | ||
.body(NO_INTERNET.toResponseBody(null)) | ||
.build() | ||
} | ||
} |
This file contains 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
This file contains 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