From b9f9d27105bdd99dd5b076b862797ca59a5925a8 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 29 Mar 2020 11:39:20 +0000 Subject: [PATCH] Catch and print all IOErrors --- mima-run/Main.hs | 2 +- src/Mima/Run.hs | 16 +--------------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/mima-run/Main.hs b/mima-run/Main.hs index feea74f..6d57744 100644 --- a/mima-run/Main.hs +++ b/mima-run/Main.hs @@ -15,7 +15,7 @@ import Mima.Vm.Storage loadMetadataOrEmpty :: FilePath -> Run Metadata loadMetadataOrEmpty path = catch (loadMetadata path) $ \e -> do - liftIO $ putStrLn $ "Metafile could not be loaded. " ++ T.unpack e + liftIO $ putStrLn $ "Metafile could not be loaded: " ++ T.unpack e pure mempty main :: IO () diff --git a/src/Mima/Run.hs b/src/Mima/Run.hs index e184c1b..4932b30 100644 --- a/src/Mima/Run.hs +++ b/src/Mima/Run.hs @@ -19,7 +19,6 @@ module Mima.Run import Control.Monad.IO.Class import Control.Monad.Trans.Except import qualified Data.ByteString.Lazy as BS -import Data.Maybe import qualified Data.Text as T import qualified Data.Text.IO as T import System.Exit @@ -50,25 +49,12 @@ catch (Run a) f = Run $ catchE a (\x -> let (Run result) = f x in result) handle :: (T.Text -> Run a) -> Run a -> Run a handle = flip catch -isRelevantError :: IOError -> Bool -isRelevantError e = isAlreadyInUseError e || isDoesNotExistError e || isPermissionError e - -formatRelevantError :: IOError -> Maybe T.Text -formatRelevantError e - | isRelevantError e = Just $ T.pack $ - "Can't open file " <> fileName <> ": " <> ioeGetErrorString e - | otherwise = Nothing - where - fileName = fromMaybe "" $ ioeGetFileName e - protect :: IO a -> Run a protect m = do result <- liftIO $ tryIOError m case result of Right a -> pure a - Left e -> case formatRelevantError e of - Nothing -> liftIO $ ioError e - Just msg -> throw msg + Left e -> throw $ T.pack $ show e readFileT :: FilePath -> Run T.Text readFileT = protect . T.readFile