Hello, I'm trying to get my mind around Arrow (0.11.0-SNAPSHOT), and trying to implement a simple app using its FP power.
I have this class that retrieves Ids from a service, and then use theses Ids to get the details in parallel, then combine the result into a list to be used in the ui layer.
The problem now once an error happens, it just crashes even though I'm handling things with Either
fold
. What I'm doing wrong in my case? and how can I prevent this code from crashing?
class GetStoriesListUseCase @Inject constructor(
private val hackerNewsService: HackerNewsService
) {
suspend fun topStories(
pageNumber: Int,
pageSize: Int = 100
): Either<Throwable, List<StoryUIModel>> =
readTopStoriesIds(pageSize)
.flatMap { getTopStoriesDetails(it) }
.suspended()
private fun getTopStoriesDetails(topStoriesIds: List<Id<Long>>): IO<Throwable, List<StoryUIModel>> =
topStoriesIds.map { it.value() }.parTraverse { anId ->
IO.effect(<http://Dispatchers.IO|Dispatchers.IO>) {
Timber.d("${Thread.currentThread().name}")
hackerNewsService.topStoryDetail(anId).toStoryUIModel()
}
}
private fun readTopStoriesIds(pageSize: Int): IO<Throwable, List<Id<Long>>> =
IO.effect {
hackerNewsService.topStoriesIds().map { Id.just(it) }.take(pageSize)
}
private fun StoryDetails.toStoryUIModel() =
StoryUIModel(id = id, title = title, author = by, url = url)
}