FetchSingleFile with Either

This commit is contained in:
Guntis Smaukstelis
2023-05-20 23:05:30 +03:00
parent b4955e2619
commit 5744f5c214
3 changed files with 19 additions and 10 deletions
+9 -6
View File
@@ -50,14 +50,17 @@ object FetchService {
}
}
def fetchSingleFile(fileName: String): IO[(String, String)] = {
fetchFiles(List(fileName)).flatMap { results =>
def fetchSingleFile(fileName: String): IO[Either[Throwable, (String, String)]] = {
fetchFiles(List(fileName)).map { results =>
results.headOption match {
case Some(Right(result)) => IO.pure(result)
case Some(Left(err)) => IO.raiseError(err)
case None => IO.raiseError(new Exception("No file fetched"))
case Some(Right(result)) =>
IO.println(s"fetched: $fileName").as(Right(result))
case Some(Left(err)) =>
IO.println(s"failed fetch: $fileName with error: ${err.getMessage}").as(Left(err))
case None =>
IO.println(s"failed fetch: $fileName").as(Left(new Exception("No file fetched")))
}
}
}.flatten
}
def fetchInRange(from: LocalDateTime, to: LocalDateTime): IO[List[Either[Throwable, (String, String)]]] = {