Skip to content
Snippets Groups Projects
Commit 5c93d48d authored by Alice Brenon's avatar Alice Brenon
Browse files

Handle errors in Main a bit more cleanly than with 'error', shutting down...

Handle errors in Main a bit more cleanly than with 'error', shutting down cleanly with the error message and no stack trace
parent da79719d
No related branches found
No related tags found
No related merge requests found
...@@ -9,21 +9,22 @@ import qualified Data.Text.IO as Text (getContents, readFile, writeFile) ...@@ -9,21 +9,22 @@ import qualified Data.Text.IO as Text (getContents, readFile, writeFile)
import Text.InvisiXML (InvisiXML(..), parse) import Text.InvisiXML (InvisiXML(..), parse)
import Text.XML.Light.Serializer (encode) import Text.XML.Light.Serializer (encode)
import System.FilePath ((<.>), dropExtension) import System.FilePath ((<.>), dropExtension)
import System.Exit (die)
endPoints :: Command -> (IO Text, FilePath) endPoints :: Command -> IO (IO Text, FilePath)
endPoints (Command {input = StdIn, outputPrefix}) = endPoints (Command {input = StdIn, outputPrefix}) =
(Text.getContents, fromMaybe noOutputPrefix outputPrefix) (,) Text.getContents <$> maybe noOutputPrefix return outputPrefix
where where
noOutputPrefix = error "output prefix (-o) is necessary when running on stdin" noOutputPrefix = die "output prefix (-o) is necessary when running on stdin"
endPoints (Command {input = FileInput f, outputPrefix}) = endPoints (Command {input = FileInput f, outputPrefix}) =
(Text.readFile f, fromMaybe (dropExtension f) outputPrefix) return (Text.readFile f, fromMaybe (dropExtension f) outputPrefix)
run :: Command -> IO () run :: Command -> IO ()
run command = run command = do
source >>= runExceptT . parse >>= either (fail . show) create (source, prefix) <- endPoints command
source >>= runExceptT . parse >>= either (fail . show) (create prefix)
where where
(source, prefix) = endPoints command create prefix (InvisiXML {text, structure}) = do
create (InvisiXML {text, structure}) = do
Text.writeFile (prefix <.> "txt") text Text.writeFile (prefix <.> "txt") text
writeFile (prefix <.> "ixml") $ encode structure writeFile (prefix <.> "ixml") $ encode structure
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment