Skip to content

Commit cba237c

Browse files
authored
Custom displayException (#30)
* hell yeah * cleanup warns * yesss * chagnelog, package
1 parent 7087e08 commit cba237c

File tree

5 files changed

+67
-9
lines changed

5 files changed

+67
-9
lines changed

ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog for `annotated-exception`
22

3+
## 0.3.0.0
4+
5+
- [#30](https://github.com/parsonsmatt/annotated-exception/pull/30)
6+
- The `Show` and `displayException` now render the annotated exception in a
7+
much nicer way.
8+
39
## 0.2.0.5
410

511
- [#27](https://github.com/parsonsmatt/annotated-exception/pull/27)

annotated-exception.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
cabal-version: 1.12
22

3-
-- This file has been generated from package.yaml by hpack version 0.35.2.
3+
-- This file has been generated from package.yaml by hpack version 0.36.0.
44
--
55
-- see: https://github.com/sol/hpack
66

77
name: annotated-exception
8-
version: 0.2.0.5
8+
version: 0.3.0.0
99
synopsis: Exceptions, with checkpoints and context.
1010
description: Please see the README on Github at <https://github.com/parsonsmatt/annotated-exception#readme>
1111
category: Control

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: annotated-exception
2-
version: 0.2.0.5
2+
version: 0.3.0.0
33
github: "parsonsmatt/annotated-exception"
44
license: BSD3
55
author: "Matt Parsons"

src/Control/Exception/Annotated.hs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{-# LANGUAGE InstanceSigs #-}
88
{-# LANGUAGE MultiParamTypeClasses #-}
99
{-# LANGUAGE RankNTypes #-}
10+
{-# LANGUAGE RecordWildCards #-}
1011
{-# LANGUAGE ScopedTypeVariables #-}
1112
{-# LANGUAGE StandaloneDeriving #-}
1213
{-# LANGUAGE TypeApplications #-}
@@ -77,7 +78,10 @@ data AnnotatedException exception
7778
{ annotations :: [Annotation]
7879
, exception :: exception
7980
}
80-
deriving (Show, Functor, Foldable, Traversable)
81+
deriving (Functor, Foldable, Traversable)
82+
83+
instance (Exception exception) => Show (AnnotatedException exception) where
84+
show = Safe.displayException
8185

8286
instance Applicative AnnotatedException where
8387
pure =
@@ -118,6 +122,31 @@ instance (Exception exception) => Exception (AnnotatedException exception) where
118122
=
119123
Nothing
120124

125+
displayException (AnnotatedException {..}) =
126+
unlines
127+
[ "! AnnotatedException !"
128+
, "Underlying exception type: " <> show (typeOf exception)
129+
, "displayException:"
130+
, "\t" <> Safe.displayException exception
131+
]
132+
<> annotationsMessage
133+
<> callStackMessage
134+
where
135+
(callStacks, otherAnnotations) = tryAnnotations @CallStack annotations
136+
callStackMessage =
137+
case listToMaybe callStacks of
138+
Nothing ->
139+
"(no callstack available)"
140+
Just cs ->
141+
prettyCallStack cs
142+
annotationsMessage =
143+
case otherAnnotations of
144+
[] ->
145+
"\n"
146+
anns ->
147+
"Annotations:\n"
148+
<> unlines (map (\ann -> "\t * " <> show ann) anns)
149+
121150
-- | Annotate the underlying exception with a 'CallStack'.
122151
--
123152
-- @since 0.2.0.0
@@ -267,13 +296,13 @@ flatten (AnnotatedException a (AnnotatedException b c)) = AnnotatedException (go
267296
addCallStackToAnnotations cs bs
268297
Nothing ->
269298
bs
270-
go mcallstack (a : as) bs =
271-
case castAnnotation a of
299+
go mcallstack (ann : anns) bs =
300+
case castAnnotation ann of
272301
Just cs ->
273302
let newAcc = fmap (mergeCallStack cs) mcallstack <|> Just cs
274-
in go newAcc as bs
303+
in go newAcc anns bs
275304
Nothing ->
276-
a : go mcallstack as bs
305+
ann : go mcallstack anns bs
277306

278307
tryFlatten :: SomeException -> SomeException
279308
tryFlatten exn =
@@ -374,7 +403,7 @@ addCallStackToException cs (AnnotatedException anns e) =
374403
AnnotatedException (addCallStackToAnnotations cs anns) e
375404

376405
addCallStackToAnnotations :: CallStack -> [Annotation] -> [Annotation]
377-
addCallStackToAnnotations cs anns = go anns
406+
addCallStackToAnnotations cs = go
378407
where
379408
-- not a huge fan of the direct recursion, but it seems easier than trying
380409
-- to finagle a `foldr` or something

test/Control/Exception/AnnotatedSpec.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@ spec = do
5555
SomeException $
5656
AnnotatedException ["hello", "goodbye"] (SomeException TestException)
5757

58+
describe "displayException" $ do
59+
it "is reasonably nice to look at" $ do
60+
lines (displayException (AnnotatedException [] TestException))
61+
`shouldBe`
62+
[ "! AnnotatedException !"
63+
, "Underlying exception type: TestException"
64+
, "displayException:"
65+
, "\tTestException"
66+
, ""
67+
, "(no callstack available)"
68+
]
69+
it "is reasonably nice to look at" $ do
70+
lines (displayException (AnnotatedException [Annotation @String "asdf"] TestException))
71+
`shouldBe`
72+
[ "! AnnotatedException !"
73+
, "Underlying exception type: TestException"
74+
, "displayException:"
75+
, "\tTestException"
76+
, "Annotations:"
77+
, "\t * Annotation @[Char] \"asdf\""
78+
, "(no callstack available)"
79+
]
80+
5881
describe "AnnotatedException can fromException a" $ do
5982
it "different type" $ do
6083
fromException (toException TestException)

0 commit comments

Comments
 (0)