mirror of
https://git.nerfingen.de/nerf/choirMail.git
synced 2025-05-15 09:50:29 +00:00
some error handling and cleanup that comes with it
This commit is contained in:
parent
99ab66b419
commit
616fc48990
7 changed files with 91 additions and 31 deletions
59
app/Main.hs
59
app/Main.hs
|
@ -16,6 +16,7 @@ import System.Environment
|
|||
import System.Exit
|
||||
import Data.Time.Format.ISO8601
|
||||
import Sender
|
||||
import Monad
|
||||
|
||||
isChoirThisWeek :: Day -> Day -> Bool
|
||||
isChoirThisWeek today day = today <= day && diffDays day today <= 6
|
||||
|
@ -23,30 +24,46 @@ isChoirThisWeek today day = today <= day && diffDays day today <= 6
|
|||
getToday :: IO Day
|
||||
getToday = utctDay <$> getCurrentTime
|
||||
|
||||
reportError :: String -> IO ()
|
||||
reportError err = hPutStr stderr err
|
||||
reportErrorLocal :: MonadIO m => String -> m ()
|
||||
reportErrorLocal err = liftIO $ hPutStr stderr err
|
||||
|
||||
reportErrorMail :: MonadIO m => Config -> String -> m ()
|
||||
reportErrorMail config error' = send (mailDomain config) (mailUsername config) (mailPassword config) (mailErrorTo config) (mailFrom config) "choirMail Error" (LT.pack error')
|
||||
|
||||
findChoirDay :: Day -> [MailRecord] -> Either String MailRecord
|
||||
findChoirDay today table = maybe
|
||||
(Left "Keine Probe :(")
|
||||
(Right)
|
||||
(L.find ((isChoirThisWeek today) .date) table)
|
||||
|
||||
|
||||
main' :: App ()
|
||||
main' = do
|
||||
args <- liftIO getArgs
|
||||
if length args /= 1
|
||||
then
|
||||
fail "We need exactly one config path as option"
|
||||
else do
|
||||
config <- parseConfigFile (head args)
|
||||
-- we want to handle these while we have the config in scope
|
||||
result <- liftIO $ runApp $ do
|
||||
bs <- request
|
||||
table <- except $ parseBString bs
|
||||
today <- liftIO getToday
|
||||
record <- except $ findChoirDay today table
|
||||
send (mailDomain config) (mailUsername config) (mailPassword config) (mailTo config) (mailFrom config) (mailSubject record) (mailText record)
|
||||
case result of
|
||||
Right x -> return x
|
||||
Left error' -> reportErrorMail config error'
|
||||
|
||||
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
if length args /= 1
|
||||
then
|
||||
die "We need exactly one argument"
|
||||
else do
|
||||
configE <- parseFile (head args)
|
||||
case configE of
|
||||
Left text -> reportError text
|
||||
Right config -> do
|
||||
bs <- request
|
||||
let eitherTable = parseBString bs
|
||||
case eitherTable of
|
||||
Left x -> reportError x
|
||||
Right table -> do
|
||||
today <- getToday
|
||||
maybe
|
||||
(T.putStr "Keine Probe :(")
|
||||
(\record -> send (mailDomain config) (mailUsername config) (mailPassword config) (mailTo config) (mailFrom config) (mailSubject record) (mailText record))
|
||||
(L.find ((isChoirThisWeek today) . date) table)
|
||||
result <- runApp main'
|
||||
case result of
|
||||
Right x -> return x
|
||||
Left error' -> reportErrorLocal error'
|
||||
|
||||
mailText :: MailRecord -> LT.Text
|
||||
mailText record = LT.fromStrict $T.concat ["Guten Morgen,\n\n"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue