-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 26 additions & 14 deletions
40
src/commonMain/kotlin/net/orandja/failure/FailureException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,55 @@ | ||
package net.orandja.failure | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
|
||
/** | ||
* Exception class representing a Failure during code execution. | ||
* This class is also a failure. | ||
* | ||
* @property failure The Failure instance associated with the exception. | ||
*/ | ||
class FailureException(val failure: Failure) : Exception(buildString { buildMessage(failure, this) }, failure.cause) { | ||
@Serializable(FailureSerializer::class) | ||
class FailureException( | ||
private val failure: Failure, | ||
) : Exception( | ||
buildString { buildMessage(failure) }, | ||
failure.cause | ||
), Failure by failure { | ||
|
||
companion object { | ||
/** | ||
* Builds the exception message based on the given [Failure]. | ||
* | ||
* @param failure The Failure instance. | ||
* @param builder The StringBuilder to append the message to. If not provided, a new StringBuilder will be created. | ||
*/ | ||
private fun buildMessage(failure: Failure, builder: StringBuilder = StringBuilder(), prepend: String = "") { | ||
private fun StringBuilder.buildMessage(failure: Failure, prepend: String = "") { | ||
if (prepend.isNotEmpty()) { | ||
builder.append('\n') | ||
builder.append(prepend) | ||
append('\n') | ||
append(prepend) | ||
} | ||
builder.append(failure.id) | ||
append(failure.id) | ||
failure.code?.let { | ||
builder.append(" [") | ||
builder.append(it) | ||
builder.append(']') | ||
append(" [") | ||
append(it) | ||
append(']') | ||
} | ||
failure.description?.let { | ||
builder.append(" (") | ||
builder.append(it) | ||
builder.append(')') | ||
append(" (") | ||
append(it) | ||
append(')') | ||
} | ||
failure.information?.let { | ||
builder.append(" ") | ||
builder.append(it) | ||
append(" ") | ||
append(it) | ||
} | ||
val attached = failure.attached ?: return | ||
attached.forEach { | ||
buildMessage(it, builder, "$prepend> ") | ||
buildMessage(it, "$prepend> ") | ||
} | ||
} | ||
} | ||
|
||
override val cause: Throwable? get() = failure.cause | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/commonTest/kotlin/net/orandja/test/assertFailureEquals.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package net.orandja.test | ||
|
||
import net.orandja.failure.Failure | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
fun assertFailureEquals(first: Failure, second: Failure) { | ||
assertEquals(first.id, second.id) | ||
assertEquals(first.code, second.code) | ||
assertEquals(first.description, second.description) | ||
assertEquals(first.information, second.information) | ||
assertEquals(first.cause, second.cause) | ||
assertContentEquals(first.attached, second.attached?.asIterable()) | ||
} |