on master help sign in
Function to print prettier parse exceptions.
8c1b69dDaniel Díaz
Data/Yaml/Internal.hs+22
@@ -54,6 +54,28 @@ data ParseException = NonScalarKey
54 deriving (Show, Typeable)
54 deriving (Show, Typeable)
55instance Exception ParseException
55instance Exception ParseException
56
56
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+
57newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
79newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
58instance Monad m => Functor (PErrorT m) where
80instance Monad m => Functor (PErrorT m) where
59 fmap = liftM
81 fmap = liftM