@@ -21,8 +21,8 @@ module Control.ActionRegistry (
2121 , withActionRegistry
2222 , unsafeNewActionRegistry
2323 , unsafeFinaliseActionRegistry
24- , CommitActionRegistryError
25- , AbortActionRegistryError
24+ , CommitActionRegistryError ( .. )
25+ , AbortActionRegistryError ( .. )
2626 , AbortActionRegistryReason
2727 -- * Registering actions #registeringActions#
2828 -- $registering-actions
@@ -61,6 +61,23 @@ import GHC.Stack
6161#define HasCallStackIfDebug ()
6262#endif
6363
64+ {- ------------------------------------------------------------------------------
65+ Printing utilities
66+ -------------------------------------------------------------------------------}
67+
68+ tabLines1 :: String -> String
69+ tabLines1 = tabLinesN 1
70+
71+ #ifdef NO_IGNORE_ASSERTS
72+ tabLines2 :: String -> String
73+ tabLines2 = tabLinesN 2
74+ #endif
75+
76+ tabLinesN :: Int -> String -> String
77+ tabLinesN n = unlines . fmap (ts++ ) . lines
78+ where
79+ ts = concat $ replicate n " "
80+
6481{- ------------------------------------------------------------------------------
6582 Modify mutable state
6683-------------------------------------------------------------------------------}
@@ -216,7 +233,15 @@ data Action m = Action {
216233
217234data ActionError = ActionError SomeException CallStack
218235 deriving stock Show
219- deriving anyclass Exception
236+
237+ instance Exception ActionError where
238+ displayException (ActionError err registerSite) = unlines [
239+ " A registered action threw an error: "
240+ , tabLines1 " The error:"
241+ , tabLines2 (displayException err)
242+ , tabLines1 " Registration site:"
243+ , tabLines2 (prettyCallStack registerSite)
244+ ]
220245
221246mkAction a = Action a callStack
222247
@@ -305,7 +330,13 @@ unsafeCommitActionRegistry reg = do
305330
306331data CommitActionRegistryError = CommitActionRegistryError (NonEmpty ActionError )
307332 deriving stock Show
308- deriving anyclass Exception
333+
334+ instance Exception CommitActionRegistryError where
335+ displayException (CommitActionRegistryError es) = unlines $ [
336+ " Exceptions thrown while committing an action registry."
337+ ] <> NE. toList (fmap displayOne es)
338+ where
339+ displayOne e = tabLines1 (displayException e)
309340
310341{-# SPECIALISE unsafeAbortActionRegistry ::
311342 ActionRegistry IO
@@ -338,7 +369,14 @@ data AbortActionRegistryReason =
338369data AbortActionRegistryError =
339370 AbortActionRegistryError AbortActionRegistryReason (NonEmpty ActionError )
340371 deriving stock Show
341- deriving anyclass Exception
372+
373+ instance Exception AbortActionRegistryError where
374+ displayException (AbortActionRegistryError reason es) = unlines $ [
375+ " Exceptions thrown while aborting an action registry."
376+ , (" Reason for aborting the registry: " ++ show reason)
377+ ] <> NE. toList (fmap displayOne es)
378+ where
379+ displayOne e = tabLines1 (displayException e)
342380
343381{-# SPECIALISE runActions :: [Action IO] -> IO [ActionError] #-}
344382-- | Run all actions even if previous actions threw exceptions.
0 commit comments