diff --git a/app/apollo/apollo-core/src/main/kotlin/com/hedvig/android/apollo/ApolloCallExt.kt b/app/apollo/apollo-core/src/main/kotlin/com/hedvig/android/apollo/ApolloCallExt.kt index 54dc98551e..dd4d9a563f 100644 --- a/app/apollo/apollo-core/src/main/kotlin/com/hedvig/android/apollo/ApolloCallExt.kt +++ b/app/apollo/apollo-core/src/main/kotlin/com/hedvig/android/apollo/ApolloCallExt.kt @@ -39,8 +39,22 @@ sealed interface ApolloOperationError { } } - data class OperationError(private val message: String) : ApolloOperationError { - override val throwable: Throwable? = null + sealed interface OperationError : ApolloOperationError { + object Unathenticated : OperationError { + override val throwable: Throwable? = null + + override fun toString(): String { + return "OperationError.Unathenticated" + } + } + + data class Other(private val message: String) : OperationError { + override val throwable: Throwable? = null + + override fun toString(): String { + return "OperationError.Other(message=$message)" + } + } } } @@ -103,6 +117,17 @@ fun ErrorMessage(apolloOperationError: ApolloOperationError): ErrorMessage = obj } } +@JvmInline +value class ExtensionErrorType(val value: String) { + companion object { + val Unauthenticated = ExtensionErrorType("UNAUTHENTICATED") + } +} + +fun Error.extensionErrorType(): ExtensionErrorType? { + return extensions?.get("errorType")?.let { ExtensionErrorType(it.toString()) } +} + // https://www.apollographql.com/docs/kotlin/essentials/errors/#truth-table private fun IorRaise>.parseResponse(response: ApolloResponse): D { val exception = response.exception @@ -129,14 +154,18 @@ private fun IorRaise>.parseRespon private fun List?.mapToOperationErrors(): Nel? { if (this == null) return null return map { error -> - ApolloOperationError.OperationError( - buildString { - append(error.message) - if (error.extensions != null) { - append(error.extensions!!.toList().joinToString(prefix = " ext: [", postfix = "]", separator = ", ")) - } - }, - ) + if (error.extensionErrorType() == ExtensionErrorType.Unauthenticated) { + ApolloOperationError.OperationError.Unathenticated + } else { + ApolloOperationError.OperationError.Other( + buildString { + append(error.message) + if (error.extensions != null) { + append(error.extensions!!.toList().joinToString(prefix = " ext: [", postfix = "]", separator = ", ")) + } + }, + ) + } }.toNonEmptyListOrNull() } @@ -145,7 +174,7 @@ private fun List?.mapToOperationErrors(): Nel? { */ private fun IorNel.iorToEither(): Either { return mapLeft { errors -> - ApolloOperationError.OperationError( + ApolloOperationError.OperationError.Other( errors.joinToString(prefix = " [", postfix = "]", separator = ", ") { it.toString() }, ) }.toEither() diff --git a/app/app/src/main/kotlin/com/hedvig/android/app/apollo/LoggingInterceptor.kt b/app/app/src/main/kotlin/com/hedvig/android/app/apollo/LoggingInterceptor.kt index 6623d442e4..0294a801b1 100644 --- a/app/app/src/main/kotlin/com/hedvig/android/app/apollo/LoggingInterceptor.kt +++ b/app/app/src/main/kotlin/com/hedvig/android/app/apollo/LoggingInterceptor.kt @@ -8,6 +8,8 @@ import com.apollographql.apollo.api.Operation.Data import com.apollographql.apollo.exception.CacheMissException import com.apollographql.apollo.interceptor.ApolloInterceptor import com.apollographql.apollo.interceptor.ApolloInterceptorChain +import com.hedvig.android.apollo.ExtensionErrorType +import com.hedvig.android.apollo.extensionErrorType import com.hedvig.android.auth.AuthTokenService import com.hedvig.android.core.tracking.ErrorSource import com.hedvig.android.core.tracking.logError @@ -103,20 +105,13 @@ private data class LoggableGraphqlError( ) data class Extensions( - val errorType: ErrorType?, + val errorType: ExtensionErrorType?, val uncategorizedExtensions: Map?, - ) { - @JvmInline - value class ErrorType(val value: String) { - companion object { - val Unauthenticated = ErrorType("UNAUTHENTICATED") - } - } - } + ) } private fun List.isUnathenticated(): Boolean { - return any { it.extensions.errorType == LoggableGraphqlError.Extensions.ErrorType.Unauthenticated } + return any { it.extensions.errorType == ExtensionErrorType.Unauthenticated } } private fun ApolloKotlinError.toGraphqlError(): LoggableGraphqlError { @@ -125,7 +120,7 @@ private fun ApolloKotlinError.toGraphqlError(): LoggableGraphqlError { locations = this.locations?.map { LoggableGraphqlError.Location(it.line, it.column) }, paths = this.path?.map { LoggableGraphqlError.Path(it.toString()) }, extensions = LoggableGraphqlError.Extensions( - errorType = this.extensions?.get("errorType")?.let { LoggableGraphqlError.Extensions.ErrorType(it.toString()) }, + errorType = this.extensionErrorType(), uncategorizedExtensions = this.extensions?.minus("errorType"), ), ) diff --git a/app/data/data-conversations/src/main/kotlin/com/hedvig/android/data/conversations/HasAnyActiveConversationUseCase.kt b/app/data/data-conversations/src/main/kotlin/com/hedvig/android/data/conversations/HasAnyActiveConversationUseCase.kt index 8d69c2c54c..00378477f7 100644 --- a/app/data/data-conversations/src/main/kotlin/com/hedvig/android/data/conversations/HasAnyActiveConversationUseCase.kt +++ b/app/data/data-conversations/src/main/kotlin/com/hedvig/android/data/conversations/HasAnyActiveConversationUseCase.kt @@ -26,6 +26,7 @@ class HasAnyActiveConversationUseCase( either { val data = result .onLeft { apolloOperationError -> + if (apolloOperationError is ApolloOperationError.OperationError.Unathenticated) return@onLeft logcat(LogPriority.ERROR, apolloOperationError.throwable) { "isEligibleToShowTheChatIcon cant determine if the chat icon should be shown. $apolloOperationError" } diff --git a/app/feature/feature-home/src/test/kotlin/com/hedvig/android/feature/home/home/ui/HomePresenterTest.kt b/app/feature/feature-home/src/test/kotlin/com/hedvig/android/feature/home/home/ui/HomePresenterTest.kt index 92001a0ac3..6155b0a690 100644 --- a/app/feature/feature-home/src/test/kotlin/com/hedvig/android/feature/home/home/ui/HomePresenterTest.kt +++ b/app/feature/feature-home/src/test/kotlin/com/hedvig/android/feature/home/home/ui/HomePresenterTest.kt @@ -47,14 +47,14 @@ internal class HomePresenterTest { assertThat(awaitItem()).isEqualTo(HomeUiState.Loading) assertThat(getHomeDataUseCase.forceNetworkFetchTurbine.awaitItem()).isFalse() - getHomeDataUseCase.responseTurbine.add(ApolloOperationError.OperationError("").left()) + getHomeDataUseCase.responseTurbine.add(ApolloOperationError.OperationError.Other("").left()) assertThat(awaitItem()).isInstanceOf() sendEvent(HomeEvent.RefreshData) assertThat(getHomeDataUseCase.forceNetworkFetchTurbine.awaitItem()).isTrue() assertThat(awaitItem()).isInstanceOf() - getHomeDataUseCase.responseTurbine.add(ApolloOperationError.OperationError("").left()) + getHomeDataUseCase.responseTurbine.add(ApolloOperationError.OperationError.Other("").left()) assertThat(awaitItem()).isInstanceOf() } } @@ -72,7 +72,7 @@ internal class HomePresenterTest { homePresenter.test(HomeUiState.Loading) { assertThat(awaitItem()).isEqualTo(HomeUiState.Loading) - getHomeDataUseCase.responseTurbine.add(ApolloOperationError.OperationError("").left()) + getHomeDataUseCase.responseTurbine.add(ApolloOperationError.OperationError.Other("").left()) assertThat(awaitItem()).isInstanceOf() sendEvent(HomeEvent.RefreshData) @@ -208,7 +208,7 @@ internal class HomePresenterTest { homePresenter.test(HomeUiState.Loading) { assertThat(awaitItem()).isEqualTo(HomeUiState.Loading) - getHomeDataUseCase.responseTurbine.add(ApolloOperationError.OperationError("").left()) + getHomeDataUseCase.responseTurbine.add(ApolloOperationError.OperationError.Other("").left()) assertThat(awaitItem()).isInstanceOf() getHomeDataUseCase.responseTurbine.add(someIrrelevantHomeDataInstance.right()) diff --git a/app/feature/feature-profile/src/test/kotlin/com/hedvig/android/feature/profile/eurobonus/EurobonusPresenterTest.kt b/app/feature/feature-profile/src/test/kotlin/com/hedvig/android/feature/profile/eurobonus/EurobonusPresenterTest.kt index 85e85f0003..d2febd3c69 100644 --- a/app/feature/feature-profile/src/test/kotlin/com/hedvig/android/feature/profile/eurobonus/EurobonusPresenterTest.kt +++ b/app/feature/feature-profile/src/test/kotlin/com/hedvig/android/feature/profile/eurobonus/EurobonusPresenterTest.kt @@ -73,7 +73,7 @@ class EurobonusPresenterTest { ), ) { awaitItem() - getEurobonusDataUseCase.responseTurbine.add(ApolloOperationError.OperationError("msg").left()) + getEurobonusDataUseCase.responseTurbine.add(ApolloOperationError.OperationError.Other("msg").left()) assertThat(awaitItem()).isEqualTo( EurobonusUiState( canSubmit = false, @@ -151,7 +151,7 @@ class EurobonusPresenterTest { awaitItem() sendEvent(EurobonusEvent.SubmitEditedEurobonus) awaitItem() - updateEurobonusNumberUseCase.responseTurbine.add(ApolloOperationError.OperationError("msg").left()) + updateEurobonusNumberUseCase.responseTurbine.add(ApolloOperationError.OperationError.Other("msg").left()) assertThat(awaitItem().hasError).isEqualTo(true) } }