David Kubecka
01/09/2024, 6:56 PMEither
(or Result
or whatever) pattern. How do you combine errors from different services/methods to a new one?
Example:
fun callTwoServices(): Either<???, String> {
val resultA: Either<ErrorA, Int> = serviceA.call()
val resultB: Either<ErrorB, Int> = serviceB.call()
// do something with the results, possibly propagating the errors
...
return Either.Right(myResult)
}
The question is what should be the ??? type?
There are two approaches I can think of
• Create a sealed class MyError
with subclasses MyErrorA
and MyErrorB
, and map the original Error*
to new ones. The resulting error type would then be MyError
.
• Create Error
sealed class already for the Error*
subclasses and use this Error
at all left positions.
Neither approach seems ideal to me. While the first one looks more correct, it's quite boilerplaty. The second one then quickly leads to a god object (containing unrelated error types).
I guess the ideal solution would be to use union type Either<MyErrorA | MyErrorB, String>
. But since this feature is not in Kotlin yet what is the approach you take in this situation?David Kubecka
01/09/2024, 6:58 PMDavid Kubecka
01/09/2024, 7:35 PMephemient
01/09/2024, 7:43 PMCartError
regardless of whether the cause was a ItemUnavailableError
or a PaymentMethodError
.David Kubecka
01/09/2024, 7:58 PMNotFound
and Unauthorized
.ephemient
01/09/2024, 8:00 PMephemient
01/09/2024, 8:00 PMephemient
01/09/2024, 8:01 PMDavid Kubecka
01/09/2024, 8:01 PMephemient
01/09/2024, 8:03 PMDavid Kubecka
01/09/2024, 8:05 PMephemient
01/09/2024, 8:08 PMDavid Kubecka
01/09/2024, 8:15 PMsealed class DomainError {
data object ErrorA: DomainError
data object ErrorB: DomainError
...
}
?
Multiple different errors might enable to you specify the same HTTP status (e.g. 404) with a specific error message.ephemient
01/09/2024, 8:16 PMAndrew O'Hara
01/09/2024, 9:49 PMross_a
01/10/2024, 9:43 AMDavid Kubecka
01/10/2024, 9:47 AMross_a
01/10/2024, 9:58 AMTies
01/10/2024, 12:44 PMephemient
01/11/2024, 12:00 AMInt | MyErrorA | MyErrorB
but both lead to some tough design decisions when it comes to how it should work with the rest of the language and runtime…