From 72e53eea4f0dc74c88d98ee20e7d657a84b536cd Mon Sep 17 00:00:00 2001 From: Dennis Frieberg Date: Fri, 6 Jun 2025 19:04:57 +0200 Subject: [PATCH] some minor code cleanup --- app/Main.hs | 11 ++++------- app/Monad.hs | 12 ++++++++++++ flake.nix | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 363759a..cc6c243 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -37,20 +37,17 @@ findChoirDay today table = maybe main' :: App () main' = do args <- liftIO getArgs - case L.uncons args of - Nothing -> fail "We need exactly one config path as option" - Just (headArgs,_) -> do + case args of + [] -> fail "Please provide a config file" + (headArgs : _) -> do config <- parseConfigFile headArgs -- we want to handle these while we have the config in scope - result <- liftIO $ runApp $ do + handleE (\error' -> reportErrorMail config error') $ 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' diff --git a/app/Monad.hs b/app/Monad.hs index 42b4593..80631cb 100644 --- a/app/Monad.hs +++ b/app/Monad.hs @@ -3,6 +3,8 @@ module Monad( App ,module Control.Monad.Fail ,module Control.Monad.IO.Class + ,handleE + ,catchE ,throwE ,except ,runApp @@ -29,6 +31,16 @@ instance MonadFail App where throwE :: String -> App a throwE = App . T.throwE +catchE :: App a -> (String -> App a) -> App a +catchE a f = do + result <- liftIO $ runApp a + case result of + Left err -> f err + Right res -> return res + +handleE :: (String -> App a) -> App a -> App a +handleE = flip catchE + except :: Either String a -> App a except = App . T.except diff --git a/flake.nix b/flake.nix index 0d3fe28..402d985 100644 --- a/flake.nix +++ b/flake.nix @@ -62,7 +62,7 @@ }); in pkgs.kakoune.override { - plugins = with pkgs.kakounePlugins; [fzf-kak kak-lsp config]; + plugins = with pkgs.kakounePlugins; [fzf-kak kakoune-lsp config]; }; in pkgs.mkShell {