P A
12/01/2022, 1:13 AMval theEither: Either<Any, D> = either {
val x : Either<A, B> = serviceA.something()
val xValue : B = x.bind()
val y : Either<C, D> = serviceB.somethingElse(x)
val yValue : D = x.bind()
yValue
}
Chris Lee
12/01/2022, 1:24 AMmapLeft
to adapt the error to something that works for the current context.Chris Lee
12/01/2022, 1:25 AMx.mapLeft { it -> OtherThing(it.message) }.bind()
P A
12/01/2022, 1:39 AMChris Lee
12/01/2022, 1:40 AMP A
12/01/2022, 1:45 AMval error: A
(or C whichever the case).P A
12/01/2022, 1:46 AMdata class SomeNewError(val description: String, val error: A) : NewHierarchy
Chris Lee
12/01/2022, 1:47 AM// outer API
public sealed interface KeyRingApiError {
public object NotFound : KeyRingApiError
public data class Error(val message: String) : KeyRingApiError
public data class Exception(val cause: Throwable) : KeyRingApiError
}
// inner API (wrapper around native calls)
internal sealed interface KeychainApiError {
object NotFound : KeychainApiError
data class Error(val errorCode: Int, val message: String) : KeychainApiError
data class Exception(val cause: Throwable) : KeychainApiError
}
private fun keyRingApiError(keychainApiError: KeychainApiError) =
when (keychainApiError) {
is KeychainApiError.NotFound -> KeyRingApiError.NotFound
is KeychainApiError.Exception -> KeyRingApiError.Exception(keychainApiError.cause)
is KeychainApiError.Error -> KeyRingApiError.Error("${keychainApiError.errorCode}: ${keychainApiError.message}")
}
Chris Lee
12/01/2022, 1:48 AMP A
12/01/2022, 1:53 AMChris Lee
12/01/2022, 1:54 AMChris Lee
12/01/2022, 1:56 AMP A
12/01/2022, 2:01 AMChris Lee
12/01/2022, 2:01 AMsimon.vergauwen
12/01/2022, 7:20 AMThrowable#cause
.Chris Lee
12/01/2022, 2:26 PMwhen(val status = callAwesomeStuff()) {
is Either.Right -> 42
is Either.Left -> when(status.value) {
is ErrorX -> when {
// uuugggghhhh.....
status.value.causes.any { it is ErrorY } -> 82
}
else -> 43
}
}
imo, chained errors:
• are necessary for diagnostic purposes
• should be removed from the type system to provide encapsulation and reduce the error-comprehension-footprint. Seems wrong to have implementation-specific errors available.Chris Lee
12/01/2022, 2:29 PM