From 3a6ebcb01d8bdc711fc91c655c8421ea08f7d361 Mon Sep 17 00:00:00 2001 From: Sean Chalmers Date: Sat, 19 Aug 2017 07:31:12 +1000 Subject: [PATCH] More comments in DB.hs Some clean up to replace use of `either` when `first` would do. --- level07/src/FirstApp/DB.hs | 5 +++++ level07/src/FirstApp/Main.hs | 7 ++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/level07/src/FirstApp/DB.hs b/level07/src/FirstApp/DB.hs index 28df14f..8279152 100644 --- a/level07/src/FirstApp/DB.hs +++ b/level07/src/FirstApp/DB.hs @@ -89,6 +89,11 @@ runDb -> AppM a runDb a = do db <- asks ( dbConn . envDb ) + -- We use the liftDb function to take the IO (Either SQLiteResponse a) and + -- convert it into an `m (Either Error a)` so that it matches the requirements + -- to be in our AppM, we then lean on the ExceptT functionality and use our + -- helper to either `throwError` with any DB errors that have made it this far + -- or simply return the desired value. liftDb db >>= throwL -- Or alternatively, if you hate variables... -- asks (dbConn.envDb) >>= ( liftDb >=> throwL ) diff --git a/level07/src/FirstApp/Main.hs b/level07/src/FirstApp/Main.hs index 2b4e5ee..39365f3 100644 --- a/level07/src/FirstApp/Main.hs +++ b/level07/src/FirstApp/Main.hs @@ -6,12 +6,13 @@ module FirstApp.Main ) where import Control.Monad.Except (ExceptT (ExceptT), - runExceptT, withExceptT) + runExceptT) import Control.Monad.IO.Class (liftIO) import Network.Wai import Network.Wai.Handler.Warp (run) +import Data.Bifunctor (first) import Data.Either (Either (..), either) import Data.Text (Text) @@ -68,8 +69,7 @@ prepareAppReqs = runExceptT $ do pure $ Env cfg db where toStartUpErr e = - withExceptT e -- apply our error constructor to unify the error types to StartUpError - . ExceptT -- convert our function to an ExceptT with the constructor + ExceptT . fmap (first e) -- Take our possibly failing configuration/db functions with their unique -- error types and turn them into a consistently typed ExceptT. We can then @@ -134,6 +134,7 @@ mkAddRequest -> Either Error RqType mkAddRequest ti c = AddRq <$> mkTopic ti + -- Got string types... <*> (mkCommentText . decodeUtf8 $ LBS.toStrict c) mkViewRequest