@@ -5,6 +5,7 @@
55module Data.Yaml.Internal
66 (
77 ParseException(..)
8+ , prettyPrintParseException
89 , parse
910 , decodeHelper
1011 , decodeHelper_
@@ -52,7 +53,45 @@ data ParseException = NonScalarKey
5253 | NonStringKeyAlias Y.AnchorName Value
5354 | CyclicIncludes
5455 deriving (Show, Typeable)
55−instance Exception ParseException
56+
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"
5695
5796newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
5897instance Monad m => Functor (PErrorT m) where