Skip to content

Commit

Permalink
toTixError will now return a copy if it's called on a tixerror
Browse files Browse the repository at this point in the history
  • Loading branch information
ncipollo committed Aug 9, 2023
1 parent f4f8b06 commit b694027
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/commonMain/kotlin/org/tix/error/TixError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ data class TixError(
override val cause: Throwable? = null
) : RuntimeException("$mood: $message", cause)

fun Throwable.toTixError() = TixError(message = this.message ?: "something bad happened", cause = this.cause)
fun Throwable.toTixError() =
if (this is TixError) {
copy()
} else {
TixError(message = this.message ?: "something bad happened", cause = this.cause)
}

fun FlowResult<*>.toTixError() = exceptionOrNull()?.toTixError() ?: TixError()
23 changes: 23 additions & 0 deletions src/commonTest/kotlin/org/tix/error/TixErrorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.tix.error

import kotlin.test.Test
import kotlin.test.expect

class TixErrorTest {
@Test
fun toTixError_alreadyATixError() {
val tixError = TixError(message = "tix_error", mood = "😬")
expect(tixError) {
tixError.toTixError()
}
}

@Test
fun toTixError_otherError() {
val cause = RuntimeException("cause")
val tixError = RuntimeException("other_error", cause)
expect(TixError(message = "other_error", cause = cause)) {
tixError.toTixError()
}
}
}

0 comments on commit b694027

Please sign in to comment.