Skip to content

Commit

Permalink
refactor: Improved error reporting, StravnikWallet error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Lastaapps committed Oct 25, 2024
1 parent 9a9f0dc commit 2e50012
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 10 deletions.
1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ internal class AndroidAgataCtuWalletApi(
.toFloatOrNull()
.bind()
}
}?.right() ?: WalletError.TotallyBroken.left()
}?.right() ?: WalletError.TotallyBroken().left()
}.flatten()
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ internal class StravnikWalletApiImpl(
}
delay(42.milliseconds)
}
raise(ApiError.WalletError.TotallyBroken)
raise(ApiError.WalletError.TotallyBroken("Failed after all the iterations"))
}
}

Expand Down Expand Up @@ -185,13 +185,19 @@ internal class StravnikWalletApiImpl(
{
log.e(it) { "Finding regex failed" }
log.e { "The error body was:\n$this" }
return ApiError.WalletError.TotallyBroken.left()
return ApiError.WalletError.TotallyBroken(
"Rexex did not match:\n" +
this@processBody
).left()
},
)
}?.replace(',', '.')
?.replace(" ", "")
?.replace(" ", "")
?.toFloatOrNull()
?.right()
?: ApiError.WalletError.TotallyBroken.left()
?: ApiError.WalletError.TotallyBroken(
"Failed to parse number:\n" +
this@processBody
).left()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024, Petr Laštovička as Lasta apps, All rights reserved
*
* This file is part of Menza.
*
* Menza is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Menza is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Menza. If not, see <https://www.gnu.org/licenses/>.
*/

package cz.lastaapps.api.agata.test

import arrow.core.Either.Right
import cz.lastaapps.menza.api.agata.api.CafeteriaApiImpl
import cz.lastaapps.menza.api.agata.api.StravnikWalletApiImpl
import cz.lastaapps.menza.api.agata.data.createAgataClient
import cz.lastaapps.menza.api.agata.data.model.AgataBEConfig
import cz.lastaapps.menza.api.agata.data.model.dto.DishTypeDto
import cz.lastaapps.menza.api.agata.data.model.dto.ServingPlaceDto
import cz.lastaapps.menza.api.agata.data.model.dto.SubsystemDto
import io.kotest.assertions.arrow.core.shouldBeRight
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.types.shouldBeInstanceOf
import io.ktor.client.HttpClient
import io.ktor.client.plugins.logging.LogLevel.BODY
import io.ktor.client.plugins.logging.Logging

class StravnikWalletTest : StringSpec(
{
fun api() = StravnikWalletApiImpl(
HttpClient {
install(Logging) {
level = BODY
}
},
)

"getBalance" {
val username = ""
val password = ""
api()
.getBalance(username, password)
.shouldBeRight()
}
},
)
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ private fun ReportDialog(
fun sendReport(
context: Context,
mode: ReportMode,
throwable: Throwable? = null,
errorText: String,
extraMessage: String?,
throwable: Throwable?,
) {
val text =
"""
Expand All @@ -251,6 +253,10 @@ fun sendReport(
|
|"Internal app problem"
|${LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)}
|${errorText}
|
|${extraMessage ?: ""}
|
|${throwable?.message ?: "Unknown error message"}
|${throwable?.stackTraceToString() ?: ""}
""".trimMargin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ private fun ReportButton(modifier: Modifier = Modifier) {

val context = LocalContext.current
ReportDialog(shown, false, { shown = false }) {
sendReport(context, it)
sendReport(context, it, "User feedback", null, null)
shown = false
}
}
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/kotlin/cz/lastaapps/menza/ui/util/HandleError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,20 @@ fun HandleError(
}

toReport?.let { errorToReport ->
val errorClass = errorToReport::class.qualifiedName
val errorText = errorToReport.text()
ReportDialog(
shown = true,
reportsCrash = true,
onDismissRequest = { toReport = null },
) {
sendReport(context, it, errorToReport.throwable)
sendReport(
context,
it,
"$errorText (class: $errorClass)",
errorToReport.extraMessage,
errorToReport.throwable,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ val ApiError.text: AppText

is WalletError ->
when (this) {
TotallyBroken -> E(R.string.error_wallet_login_failed_critical)
is TotallyBroken -> E(R.string.error_wallet_login_failed_critical)
InvalidCredentials -> E(R.string.error_wallet_login_failed_credentials)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ sealed interface ApiError : DomainError.Logic {
}

sealed interface WalletError : ApiError {
data object TotallyBroken : WalletError
@JvmInline
value class TotallyBroken(override val extraMessage: String? = null) : WalletError

data object InvalidCredentials : WalletError
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ sealed interface DomainError {
val throwable: Throwable?
get() = null

val extraMessage: String?
get() = null

sealed interface Runtime : DomainError

sealed interface Logic : DomainError
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ kermit = "2.0.4"
koin = "4.0.0"
koin-annotations = "1.0.3"
kotest = "5.9.1"
kotest-arrow = "1.4.0"
kotlin = "2.0.20"
# @keep
kotlin-api = "2.0"
Expand Down Expand Up @@ -129,6 +130,7 @@ koin-ktorServer = { module = "io.insert-koin:koin-ktor", version.ref = "koin" }
koin-ktorServer-logger = { module = "io.insert-koin:koin-logger-slf4j", version.ref = "koin" }
koin-test-core = { module = "io.insert-koin:koin-test", version.ref = "koin" }
koin-test-jUnit5 = { module = "io.insert-koin:koin-test-junit5", version.ref = "koin" }
kotest-arrow = { module = "io.kotest.extensions:kotest-assertions-arrow", version.ref = "kotest-arrow" }
kotest-assertion = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
kotest-jUnit5runner = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
kotest-property = { module = "io.kotest:kotest-property", version.ref = "kotest" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class JvmAppConvention :
testImplementation(libs.kotlin.test.annotation)
testImplementation(libs.kotlin.test.common)
testImplementation(libs.kotlin.test.core)
implementation(libs.kotest.arrow)
testImplementation(libs.kotest.assertion)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.kotest.jUnit5runner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class KMPLibraryConvention :
// implementation(libs.kotlin.test.common)
// implementation(libs.kotlin.test.core)
// implementation(libs.kotlin.test.jUnit5)
implementation(libs.kotest.arrow)
implementation(libs.kotest.assertion)
implementation(libs.kotlinx.coroutines.test)
// implementation(libs.koin.test.jUnit5)
Expand Down

0 comments on commit 2e50012

Please sign in to comment.