https://kotlinlang.org logo
#arrow
Title
# arrow
r

rcd27

12/22/2022, 7:26 AM
Greetings. We wrap all "external API calls` in
Either.catch
(since we can not control the network layer of external libs, and moreover they throw exceptions). Then we map
left
block to our domain errors. The question is: is it OK to apply
.fold
functor to
Either
in case, when
Left
can hold an actual result we need? The code seems like that:
Copy code
...
.fold(
                ifLeft = {
                    when (it) {
                        ExternalApiError.NotFound -> emptyListOf<SourceSnippet>().right()
                    }
                },
                ifRight = { it.right() },
            )
So here, when we got an external error, we treat it like
nothing found
, but this is "green way"
s

simon.vergauwen

12/22/2022, 7:50 AM
Hey @rcd27, That is perfectly fine, but there are probably better functions for doing that 🙂 You can use:
Copy code
handleErrorWith { e ->
  when(e) {
    ExternalApiError.NotFound -> emptyListOf<SourceSnippet>().right()
   else -> e.left()
  }
}
Or
Copy code
handleError { _ -> emptyListOf<SourceSnippet>() }
r

rcd27

12/22/2022, 7:54 AM
Thank you, @simon.vergauwen
9 Views