@@ -21,6 +21,7 @@ module Katip.Monadic
23 , logItemM
23 , logItemM
25 -- * Machinery for merging typed log payloads/contexts
26 -- * Machinery for merging typed log payloads/contexts
26 , KatipContext(..)
27 , KatipContext(..)
@@ -64,6 +65,7 @@ import qualified Control.Monad.Trans.Writer.Strict as Strict (WriterT)
64import Data.Aeson
65import Data.Aeson
65import qualified Data.HashMap.Strict as HM
66import qualified Data.HashMap.Strict as HM
66import Data.Monoid
67import Data.Monoid
68+import Data.Text (Text)
67import Language.Haskell.TH
69import Language.Haskell.TH
68-------------------------------------------------------------------------------
70-------------------------------------------------------------------------------
69import Katip.Core
71import Katip.Core
@@ -179,6 +181,22 @@ logTM :: ExpQ
179logTM = [| logItemM (Just $(getLoc)) |]
181logTM = [| logItemM (Just $(getLoc)) |]
184+-------------------------------------------------------------------------------
185+-- | Perform an action while logging any exceptions that may occur.
186+-- Inspired by 'onException`.
188+-- >>>> error "foo" `logExceptionM` ErrorS
190+ :: (KatipContext m, MonadCatch m, Applicative m)
191+ => m a -- ^ Main action to run
192+ -> Severity -- ^ Severity
194+logExceptionM action sev = action `catchAll` \e -> f e >> throwM e
196+ f e = logFM sev (msg e)
197+ msg e = ls ("An exception has occured: " :: Text) <> showLS e
182-------------------------------------------------------------------------------
200-------------------------------------------------------------------------------
183-- | Wrapper over 'ReaderT' that provides a passthrough instance for
201-- | Wrapper over 'ReaderT' that provides a passthrough instance for
184-- 'MonadReader' so your stack will not be affected. Also provides a
202-- 'MonadReader' so your stack will not be affected. Also provides a