Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce9480e0 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Introduce CustomException

parent 28fcea9b
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -18,9 +18,9 @@

package foundation.e.apps.data

import foundation.e.apps.data.login.exceptions.CustomException
import foundation.e.apps.data.playstore.utils.GPlayHttpClient
import foundation.e.apps.data.playstore.utils.GplayHttpRequestException
import foundation.e.apps.data.login.exceptions.GPlayException
import kotlinx.coroutines.delay
import timber.log.Timber
import java.net.SocketTimeoutException
@@ -55,9 +55,15 @@ private fun <T> handleSocketTimeoutException(e: SocketTimeoutException): ResultS

private fun <T> resultSupremeGplayHttpRequestException(e: GplayHttpRequestException): ResultSupreme<T> {
    val message = extractErrorMessage(e)
    val exception = GPlayException(e.status == GPlayHttpClient.STATUS_CODE_TIMEOUT, message)
    val error = if (e.status == GPlayHttpClient.STATUS_CODE_TIMEOUT) {
        CustomException.Error.TIMEOUT_ERROR
    } else {
        CustomException.Error.UNKNOWN_ERROR
    }

    val exception = CustomException(error, message)

    return if (exception.isTimeout) {
    return if (error == CustomException.Error.TIMEOUT_ERROR) {
        ResultSupreme.Timeout(exception = exception)
    } else {
        ResultSupreme.Error(message, exception)
+5 −4
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
package foundation.e.apps.data

import foundation.e.apps.data.enums.ResultStatus
import foundation.e.apps.data.login.exceptions.CustomException
import java.util.concurrent.TimeoutException

private const val UNKNOWN_ERROR = "Unknown error!"
@@ -60,7 +61,7 @@ sealed class ResultSupreme<T> {
            data?.let {
                setData(it)
            }
            this.exception = exception
            this.exception?.encapsulatedException = exception
        }
    }

@@ -76,7 +77,7 @@ sealed class ResultSupreme<T> {
         */
        constructor(message: String, exception: Exception? = null) : this() {
            this.message = message
            this.exception = exception
            this.exception?.encapsulatedException = exception
        }

        /**
@@ -111,7 +112,7 @@ sealed class ResultSupreme<T> {
    /**
     * Exception from try-catch block for error cases.
     */
    var exception: Exception? = null
    var exception: CustomException? = null

    fun isValidData() = data != null

@@ -155,7 +156,7 @@ sealed class ResultSupreme<T> {
                    this.data = data
                } else {
                    this.message = message.ifBlank { status.message }
                    this.exception = exception
                    this.exception?.encapsulatedException = exception
                }
            }
            return resultObject
+9 −7
Original line number Diff line number Diff line
@@ -21,8 +21,7 @@ import com.aurora.gplayapi.data.models.AuthData
import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.enums.User
import foundation.e.apps.data.login.AuthObject.GPlayAuth
import foundation.e.apps.data.login.exceptions.CleanApkException
import foundation.e.apps.data.login.exceptions.GPlayValidationException
import foundation.e.apps.data.login.exceptions.CustomException

/**
 * Auth objects define which sources data is to be loaded from, for each source, also provides
@@ -48,10 +47,12 @@ sealed class AuthObject {
            return GPlayAuth(
                ResultSupreme.Error<AuthData?>(
                    message = message,
                    exception = GPlayValidationException(
                    exception = CustomException(
                        CustomException.Error.AUTHENTICATION_ERROR,
                        message,
                        this.user,
                        401,
                        CustomException.Type.GOOGLE_PLAY
                    )
                ).apply {
                    otherPayload = this@GPlayAuth.result.otherPayload
@@ -66,14 +67,15 @@ sealed class AuthObject {
            return CleanApk(
                ResultSupreme.Error(
                    message = "Unauthorized",
                    exception = CleanApkException(
                        isTimeout = false,
                        message = "Unauthorized",
                    exception = CustomException(
                        CustomException.Error.AUTHENTICATION_ERROR,
                        "Unauthorized",
                        this.user
                    )
                ),
                this.user,
            )
        }
    }
    // Add more auth types here

}
+7 −2
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ package foundation.e.apps.data.login
import com.aurora.gplayapi.data.models.AuthData
import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.enums.User
import foundation.e.apps.data.login.exceptions.GPlayLoginException
import foundation.e.apps.data.login.exceptions.CustomException
import javax.inject.Inject
import javax.inject.Singleton

@@ -34,7 +34,12 @@ class AuthenticatorRepository @Inject constructor(
    private var gPlayAuth: AuthData? = null

    fun getGPlayAuthOrThrow(): AuthData {
        return gPlayAuth ?: throw GPlayLoginException(false, "AuthData is not available!", getUserType())
        return gPlayAuth ?: throw CustomException(
            CustomException.Error.AUTHENTICATION_ERROR,
            "AuthData is not available!",
            getUserType(),
            type = CustomException.Type.GOOGLE_PLAY
        )
    }

    fun setGPlayAuth(auth: AuthData) {
+39 −7
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.enums.User
import foundation.e.apps.data.playstore.utils.AC2DMUtil
import foundation.e.apps.data.handleNetworkResult
import foundation.e.apps.data.login.exceptions.GPlayLoginException
import foundation.e.apps.data.login.exceptions.CustomException
import foundation.e.apps.utils.eventBus.AppEvent
import foundation.e.apps.utils.eventBus.EventBus
import java.util.Locale
@@ -52,8 +52,20 @@ class PlayStoreLoginWrapper constructor(
        return result.apply {
            this.data?.locale = locale
            this.exception = when (result) {
                is ResultSupreme.Timeout -> GPlayLoginException(true, "GPlay API timeout", user)
                is ResultSupreme.Error -> GPlayLoginException(false, result.message, user)
                is ResultSupreme.Timeout ->
                    CustomException(
                        CustomException.Error.TIMEOUT_ERROR,
                        "GPlay API timeout",
                        user,
                        type = CustomException.Type.GOOGLE_PLAY
                    )
                is ResultSupreme.Error ->
                    CustomException(
                        CustomException.Error.AUTHENTICATION_ERROR,
                        result.message,
                        user,
                        type = CustomException.Type.GOOGLE_PLAY
                    )
                else -> {
                    EventBus.invokeEvent(AppEvent.SuccessfulLogin(user))
                    null
@@ -82,8 +94,19 @@ class PlayStoreLoginWrapper constructor(
        }
        return ResultSupreme.replicate(result, response).apply {
            this.exception = when (result) {
                is ResultSupreme.Timeout -> GPlayLoginException(true, "GPlay API timeout", user)
                is ResultSupreme.Error -> GPlayLoginException(false, result.message, user)
                is ResultSupreme.Timeout ->
                    CustomException(
                        CustomException.Error.TIMEOUT_ERROR,
                        "GPlay API timeout",
                        user,
                        type = CustomException.Type.GOOGLE_PLAY
                    )
                is ResultSupreme.Error -> CustomException(
                    CustomException.Error.AUTHENTICATION_ERROR,
                    result.message,
                    user,
                    type = CustomException.Type.GOOGLE_PLAY
                )
                else -> null
            }
        }
@@ -129,8 +152,17 @@ class PlayStoreLoginWrapper constructor(
        }
        return result.apply {
            this.exception = when (result) {
                is ResultSupreme.Timeout -> GPlayLoginException(true, "GPlay API timeout", User.GOOGLE)
                is ResultSupreme.Error -> GPlayLoginException(false, result.message, User.GOOGLE)
                is ResultSupreme.Timeout -> CustomException(
                    CustomException.Error.TIMEOUT_ERROR,
                    "GPlay API timeout",
                    User.GOOGLE,
                    type = CustomException.Type.GOOGLE_PLAY
                )
                is ResultSupreme.Error -> CustomException(
                    CustomException.Error.AUTHENTICATION_ERROR,
                    result.message,
                    User.GOOGLE,
                    type = CustomException.Type.GOOGLE_PLAY)
                else -> null
            }
        }
Loading