Skip to content

Commit e64ccd6

Browse files
committed
Display action registry exceptions in human-readable format
1 parent 3337664 commit e64ccd6

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src-control/Control/ActionRegistry.hs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

217234
data 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

221246
mkAction a = Action a callStack
222247

@@ -305,7 +330,13 @@ unsafeCommitActionRegistry reg = do
305330

306331
data 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 =
338369
data 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.

test-control/Test/Control/ActionRegistry.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ prop_commitActionRegistryError = once $ ioProperty $ do
2121
delayedCommit reg
2222
(throwIO (userError "delayed action failed"))
2323
pure $ case eith of
24-
Left e -> tabulate "Printed error" [show e] $ property True
24+
Left e ->
25+
tabulate "displayException" [displayExceptionNewline e] $ property True
2526
Right () -> property False
2627

2728
-- | An example where an exception happens while an action registry is being
@@ -36,5 +37,9 @@ prop_abortActionRegistryError = once $ ioProperty $ do
3637
(\_ -> throwIO (userError "rollback action failed"))
3738
throwIO (userError "error in withActionRegistry scope")
3839
pure $ case eith of
39-
Left e -> tabulate "Printed error" [show e] $ property True
40+
Left e ->
41+
tabulate "displayException" [displayExceptionNewline e] $ property True
4042
Right () -> property False
43+
44+
displayExceptionNewline :: Exception e => e -> String
45+
displayExceptionNewline e = '\n':displayException e

0 commit comments

Comments
 (0)