on master help sign in
Function to print prettier parse exceptions.
commit8c1b69dd502f4b24686010e2a058696f167c229d authorDaniel Díaz <dhelta.diaz@gmail.com> dateTue 31 Mar 2015, 12:08 (12 years ago) parenta9f4c71
1 file changed+22
Data/Yaml/Internal.hs+22
Data/Yaml/Internal.hs+22
@@ -54,6 +54,28 @@ data ParseException = NonScalarKey
5454 deriving (Show, Typeable)
5555instance Exception ParseException
5656
57+-- | Alternative to 'show' to display a 'ParseException' on the screen.
58+-- Instead of displaying the data constructors applied to their arguments,
59+-- a more textual output is returned. For example, instead of printing:
60+--
61+-- > AesonException "The key \"foo\" was not found"
62+--
63+-- It looks more pleasant to print:
64+--
65+-- > Aeson exception: The key "foo" was not found
66+--
67+prettyPrintParseException :: ParseException -> String
68+prettyPrintParseException NonScalarKey = "Non scalar key"
69+prettyPrintParseException InvalidYaml mye =
70+ case mye of
71+ Just ye -> "Invalid yaml: " ++ show ye
72+ _ -> "Invalid yaml"
73+prettyPrintParseException (AesonException e) =
74+ "Aeson exception: " ++ e
75+prettyPrintParseException pe = show pe
76+-- TODO: This is just a stab at the implementation.
77+-- If the idea is likely to be merged, I can develop it further.
78+
5779newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
5880instance Monad m => Functor (PErrorT m) where
5981 fmap = liftM