on master help sign in
Merge pull request #59 from Daniel-Diaz/master
f88b016Michael Snoyman
Data/Yaml.hs+1
@@ -24,6 +24,7 @@ module Data.Yaml
24 , Object
24 , Object
25 , Array
25 , Array
26 , ParseException(..)
26 , ParseException(..)
27+ , prettyPrintParseException
27 , YamlException (..)
28 , YamlException (..)
28 , YamlMark (..)
29 , YamlMark (..)
29 -- * Constructors and accessors
30 -- * Constructors and accessors
Data/Yaml/Internal.hs+40−1
@@ -5,6 +5,7 @@
5module Data.Yaml.Internal
5module Data.Yaml.Internal
6 (
6 (
7 ParseException(..)
7 ParseException(..)
8+ , prettyPrintParseException
8 , parse
9 , parse
9 , decodeHelper
10 , decodeHelper
10 , decodeHelper_
11 , decodeHelper_
@@ -52,7 +53,45 @@ data ParseException = NonScalarKey
52 | NonStringKeyAlias Y.AnchorName Value
53 | NonStringKeyAlias Y.AnchorName Value
53 | CyclicIncludes
54 | CyclicIncludes
54 deriving (Show, Typeable)
55 deriving (Show, Typeable)
56+
55−instance Exception ParseException
57+instance Exception ParseException where
58+#if MIN_VERSION_base(4, 8, 0)
59+ displayException = prettyPrintParseException
60+#endif
61+
62+-- | Alternative to 'show' to display a 'ParseException' on the screen.
63+-- Instead of displaying the data constructors applied to their arguments,
64+-- a more textual output is returned. For example, instead of printing:
65+--
66+-- > AesonException "The key \"foo\" was not found"
67+--
68+-- It looks more pleasant to print:
69+--
70+-- > Aeson exception: The key "foo" was not found
71+--
72+prettyPrintParseException :: ParseException -> String
73+prettyPrintParseException NonScalarKey = "Non scalar key"
74+prettyPrintParseException (UnknownAlias n) =
75+ "Unknown alias: " ++ n
76+prettyPrintParseException (UnexpectedEvent r e) = unlines
77+ [ "Unexpected event:"
78+ , " Received: " ++ maybe "None" show r
79+ , " Expected: " ++ maybe "None" show e
80+ ]
81+prettyPrintParseException (InvalidYaml mye) =
82+ case mye of
83+ Just ye -> "Invalid yaml: " ++ show ye
84+ _ -> "Invalid yaml"
85+prettyPrintParseException (AesonException e) =
86+ "Aeson exception: " ++ e
87+prettyPrintParseException (OtherParseException e) =
88+ "Parse exception: " ++ show e
89+prettyPrintParseException (NonStringKeyAlias n v) = unlines
90+ [ "Non-string key alias:"
91+ , " Anchor name: " ++ n
92+ , " Value: " ++ show v
93+ ]
94+prettyPrintParseException CyclicIncludes = "Cyclic includes"
56
95
57newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
96newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
58instance Monad m => Functor (PErrorT m) where
97instance Monad m => Functor (PErrorT m) where