Catch exception when reading file

This commit is contained in:
Rodrigo Fernandes 2016-07-24 13:56:45 +01:00
parent 127e73abe9
commit 88cd8909a3
No known key found for this signature in database
GPG Key ID: 08E3C5F38969078E
1 changed files with 11 additions and 3 deletions

View File

@ -849,9 +849,17 @@ object Cache {
logger = logger,
pool = pool,
ttl = ttl
).leftMap(_.describe).map { f =>
// FIXME Catch error here?
new String(NioFiles.readAllBytes(f.toPath), "UTF-8")
).leftMap(_.describe).flatMap { f =>
val res = if (!f.isDirectory && f.exists()) {
Try(new String(NioFiles.readAllBytes(f.toPath), "UTF-8")) match {
case scala.util.Success(content) =>
Right(content)
case scala.util.Failure(e) =>
Left(s"Could not read (file:${f.getCanonicalPath}): ${e.getMessage}")
}
} else Left(s"Could not read (file:${f.getCanonicalPath}) (isFile:${!f.isDirectory}) (exists:${f.exists()})")
EitherT.fromEither(Task.now[Either[String, String]](res))
}
}