aboutsummaryrefslogtreecommitdiffstats
path: root/src/Utils.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Utils.hs')
-rw-r--r--src/Utils.hs19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/Utils.hs b/src/Utils.hs
index bcac58e..2f9e3c0 100644
--- a/src/Utils.hs
+++ b/src/Utils.hs
@@ -2,6 +2,7 @@
module Utils where
import Control.Monad.Except
+import Control.Exception (IOException, catch)
import Control.Monad.State
import qualified Data.Map as M
import qualified Data.List as L
@@ -301,3 +302,21 @@ separateNsIdPart identifier =
nsPartText = T.concat $ L.init parts
idPartText = L.last parts
in (T.unpack nsPartText, T.unpack idPartText)
+
+safeReadFile :: FilePath -> IO (Maybe String)
+safeReadFile p = (Just <$> readFile p) `catch` handler
+ where
+ handler :: IOException -> IO (Maybe String)
+ handler _ = pure Nothing
+
+safeWriteFile :: FilePath -> String -> IO (Maybe ())
+safeWriteFile p content = (Just <$> writeFile p content) `catch` handler
+ where
+ handler :: IOException -> IO (Maybe ())
+ handler _ = pure Nothing
+
+safeAppendFile :: FilePath -> String -> IO (Maybe ())
+safeAppendFile p content = (Just <$> appendFile p content) `catch` handler
+ where
+ handler :: IOException -> IO (Maybe ())
+ handler _ = pure Nothing