Greetings. We wrap all "external API calls` in `Ei...
# arrow
r
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
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
Thank you, @simon.vergauwen