package info.fotm.util

import java.nio.file.{Files, Paths}

import com.twitter.bijection.Bijection

import scala.util.Try

class FilePersisted[S](fileName: String)(implicit serializer: Bijection[S, Array[Byte]]) extends Persisted[S] {

  override def save(state: S): Unit = {
    Files.write(Paths.get(fileName), serializer(state))
  }

  override def fetch(): Option[S] =
    Try {
      val bytes = Files.readAllBytes(Paths.get(fileName))
      serializer.inverse(bytes)
    }.toOption
}

package info.fotm.util

import java.nio.file.{Files, Paths}

import com.twitter.bijection.Bijection

class FilePersisted[S](fileName: String)(implicit serializer: Bijection[S, Array[Byte]]) extends Persisted[S] {

  override def save(state: S): Unit = {
    Files.write(Paths.get(fileName), serializer(state))
  }

  override def fetch(): Option[S] =
    if (Files.exists(Paths.get(fileName))) {
      val bytes = Files.readAllBytes(Paths.get(fileName))
      Some(serializer.inverse(bytes))
    } else {
      None
    }
}

--- 
+++ 
@@ -3,6 +3,8 @@
 import java.nio.file.{Files, Paths}
 
 import com.twitter.bijection.Bijection
+
+import scala.util.Try
 
 class FilePersisted[S](fileName: String)(implicit serializer: Bijection[S, Array[Byte]]) extends Persisted[S] {
 
@@ -11,10 +13,8 @@
   }
 
   override def fetch(): Option[S] =
-    if (Files.exists(Paths.get(fileName))) {
+    Try {
       val bytes = Files.readAllBytes(Paths.get(fileName))
-      Some(serializer.inverse(bytes))
-    } else {
-      None
-    }
+      serializer.inverse(bytes)
+    }.toOption
 }
Fix fetch from file when there's an exception
