Skip to content

Commit 3f2032e

Browse files
committed
#36 improve onEach api
1 parent 6d5f2fc commit 3f2032e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

errors/src/commonMain/kotlin/dev/icerock/moko/errors/mappers/ExceptionMappersStorage.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object ExceptionMappersStorage {
2020
fallback = { MR.strings.moko_errors_unknownError.desc() }
2121
)
2222
)
23-
private val notifiers: MutableList<(Throwable, KClass<*>) -> Unit> = mutableListOf()
23+
private val notifiers: MutableList<(Throwable, KClass<*>, Any) -> Unit> = mutableListOf()
2424

2525
private fun <T : Any> getOrCreateContainer(resultClass: KClass<T>): MappersContainer<T> {
2626
val existContainer: MappersContainer<*>? = containers[resultClass]
@@ -132,10 +132,11 @@ object ExceptionMappersStorage {
132132
?: container.fallback
133133

134134
return { exception ->
135+
val result: T = mapper(exception)
135136
notifiers.forEach { notifier ->
136-
notifier(exception, resultClass)
137+
notifier(exception, resultClass, result)
137138
}
138-
mapper(exception)
139+
result
139140
}
140141
}
141142

@@ -208,7 +209,7 @@ object ExceptionMappersStorage {
208209
* @param block - lambda that will be called when exception map to some class
209210
*/
210211
fun onEach(
211-
block: (Throwable, KClass<*>) -> Unit
212+
block: (Throwable, KClass<*>, Any) -> Unit
212213
): ExceptionMappersStorage {
213214
notifiers.add(block)
214215
return this

sample/mpp-library/src/commonMain/kotlin/com/icerockdev/library/ExceptionStorage.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ fun initExceptionStorage() {
1414
.register<IllegalArgumentException, StringDesc> {
1515
MR.strings.illegalArgumentText.desc()
1616
}
17+
.onEach { throwable, kClass, result ->
18+
println("$throwable mapped to $kClass with result $result")
19+
}
1720
}

0 commit comments

Comments
 (0)