handleDBException :: SomeException -> HandlerFor site (EitherDatabaseException a) handleDBException e = let eMsg = T.pack $ show e in if"Duplicate" `T.isInfixOf` eMsg then return $ LeftDuplicateEntryException else return $ Left $ GeneralDatabaseException eMsg
tryDbAction :: YesodDB site a -> HandlerFor site (EitherDatabaseException a) tryDbAction dbAction = ( Right <$> dbAction) `catch` handleDBException
上述函数并没有使用到HandlerFor这个类型相关的函数,完全可以用Monad约束代替。
1 2 3 4 5 6 7 8 9 10
handleDBException :: Monad m => SomeException -> m (EitherDatabaseException a) handleDBException e = let eMsg = T.pack $ show e in if"Duplicate" `T.isInfixOf` eMsg then return $ LeftDuplicateEntryException else return $ Left $ GeneralDatabaseException eMsg
tryDbAction :: (Functor m, Monad m, MonadUnliftIO m) => m a -> m (EitherDatabaseException a) tryDbAction dbAction = ( Right <$> dbAction) `catch` handleDBException
newtype
在构建newtype的时候也是一样,例如构建一个ReaderT
1 2
newtypeServeFor site param a = ServeFor (ReaderT (ServiceContextparam) (HandlerForsite) a) deriving (Functor, Applicative, Monad, MonadIO, MonadResource, MonadLogger)
也可以修改如下
1
newtypeMonad m => ServeFor param m a = ServeFor (ReaderT (ServiceContextparam) m a)