|
| 1 | +package com.sysadmindoc.callshield.data.repository |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.squareup.moshi.JsonDataException |
| 5 | +import com.squareup.moshi.JsonEncodingException |
| 6 | +import com.sysadmindoc.callshield.R |
| 7 | +import com.sysadmindoc.callshield.data.remote.GitHubFeedFailureReason |
| 8 | +import com.sysadmindoc.callshield.data.remote.GitHubFeedHttpException |
| 9 | +import com.sysadmindoc.callshield.data.remote.GitHubFeedValidationException |
| 10 | +import java.io.IOException |
| 11 | +import java.net.UnknownHostException |
| 12 | +import javax.net.ssl.SSLException |
| 13 | + |
| 14 | +internal enum class SyncFailureKind { |
| 15 | + OFFLINE, |
| 16 | + DNS, |
| 17 | + SECURE_CONNECTION, |
| 18 | + HTTP, |
| 19 | + SIGNATURE, |
| 20 | + PARSE, |
| 21 | + UNKNOWN, |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Turns a failed database sync into one plain sentence. The exception text |
| 26 | + * ("Unable to resolve host ...", "HTTP 404: Not Found") used to be the message |
| 27 | + * Home showed, so SyncRepository now logs it and shows this instead. |
| 28 | + */ |
| 29 | +internal object SyncFailureText { |
| 30 | + private const val MAX_CAUSE_DEPTH = 8 |
| 31 | + private const val EMPTY_BODY = "Empty response body" |
| 32 | + private val httpStatus = Regex("""\bHTTP (\d{3})\b""") |
| 33 | + |
| 34 | + /** A feed host that refuses the request this way will refuse the retry too. */ |
| 35 | + private val permanentHttpCodes = setOf(400, 401, 403, 404) |
| 36 | + |
| 37 | + fun kind(error: Throwable?): SyncFailureKind = causes(error).firstNotNullOfOrNull(::kindOf) ?: SyncFailureKind.UNKNOWN |
| 38 | + |
| 39 | + fun httpCode(error: Throwable?): Int? = |
| 40 | + causes(error).firstNotNullOfOrNull { cause -> |
| 41 | + (cause as? GitHubFeedHttpException)?.code |
| 42 | + ?: cause.message?.let { message -> |
| 43 | + httpStatus |
| 44 | + .find(message) |
| 45 | + ?.groupValues |
| 46 | + ?.get(1) |
| 47 | + ?.toIntOrNull() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fun shouldRetry(error: Throwable?): Boolean = httpCode(error) !in permanentHttpCodes |
| 52 | + |
| 53 | + fun sentence( |
| 54 | + context: Context, |
| 55 | + error: Throwable?, |
| 56 | + ): String = |
| 57 | + when (kind(error)) { |
| 58 | + SyncFailureKind.OFFLINE -> context.getString(R.string.sync_failure_offline) |
| 59 | + SyncFailureKind.DNS -> context.getString(R.string.sync_failure_dns) |
| 60 | + SyncFailureKind.SECURE_CONNECTION -> context.getString(R.string.sync_failure_secure_connection) |
| 61 | + SyncFailureKind.HTTP -> context.getString(R.string.sync_failure_http, httpCode(error) ?: 0) |
| 62 | + SyncFailureKind.SIGNATURE -> context.getString(R.string.sync_failure_signature) |
| 63 | + SyncFailureKind.PARSE -> context.getString(R.string.sync_failure_parse) |
| 64 | + SyncFailureKind.UNKNOWN -> context.getString(R.string.sync_unknown_error) |
| 65 | + } |
| 66 | + |
| 67 | + private fun causes(error: Throwable?): List<Throwable> = generateSequence(error) { it.cause }.take(MAX_CAUSE_DEPTH).toList() |
| 68 | + |
| 69 | + // Moshi's JsonEncodingException is an IOException, so parse failures are |
| 70 | + // matched before the network ones. |
| 71 | + private fun kindOf(error: Throwable): SyncFailureKind? = |
| 72 | + when (error) { |
| 73 | + is GitHubFeedValidationException -> { |
| 74 | + if (error.reason == GitHubFeedFailureReason.SIGNATURE) SyncFailureKind.SIGNATURE else SyncFailureKind.PARSE |
| 75 | + } |
| 76 | + |
| 77 | + is SpamFeedManifestRejectedException, is JsonDataException, is JsonEncodingException -> { |
| 78 | + SyncFailureKind.PARSE |
| 79 | + } |
| 80 | + |
| 81 | + is GitHubFeedHttpException -> { |
| 82 | + SyncFailureKind.HTTP |
| 83 | + } |
| 84 | + |
| 85 | + is UnknownHostException -> { |
| 86 | + SyncFailureKind.DNS |
| 87 | + } |
| 88 | + |
| 89 | + is SSLException -> { |
| 90 | + SyncFailureKind.SECURE_CONNECTION |
| 91 | + } |
| 92 | + |
| 93 | + is IOException -> { |
| 94 | + SyncFailureKind.OFFLINE |
| 95 | + } |
| 96 | + |
| 97 | + else -> { |
| 98 | + val message = error.message.orEmpty() |
| 99 | + when { |
| 100 | + httpStatus.containsMatchIn(message) -> SyncFailureKind.HTTP |
| 101 | + message == EMPTY_BODY -> SyncFailureKind.PARSE |
| 102 | + else -> null |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments