on master help sign in
Merge pull request #67 from bitonic/pretty-print-improvements

Pretty print improvements

commit8db0d580c3cd172df5ffb91977ce3c5af8f595b1 authorMichael Snoyman <michael@snoyman.com> dateSat 29 Aug 2015, 21:10 (11 years ago) parentbd682eeaa1dce9
2 files changed+32−23
Data/Yaml.hs+1−1 Data/Yaml/Internal.hs+31−22
Data/Yaml.hs+1−1
@@ -167,7 +167,7 @@ decodeFileEither = decodeHelper_ . Y.decodeFile
167167
168168decodeEither :: FromJSON a => ByteString -> Either String a
169169decodeEither bs = unsafePerformIO
170− $ fmap (either (Left . show) id)
170+ $ fmap (either (Left . prettyPrintParseException) id)
171171 $ decodeHelper (Y.decode bs)
172172
173173-- | More helpful version of 'decodeEither' which returns the 'YamlException'.
Data/Yaml/Internal.hs+31−22
@@ -19,6 +19,7 @@ import Data.Aeson.Types hiding (parse)
1919import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
2020import Data.ByteString (ByteString)
2121import qualified Data.Map as Map
22+import Data.Maybe (isNothing)
2223import Control.Exception
2324import Control.Exception.Enclosed
2425import Control.Monad.Trans.State
@@ -66,36 +67,44 @@ instance Exception ParseException where
6667-- Instead of displaying the data constructors applied to their arguments,
6768-- a more textual output is returned. For example, instead of printing:
6869--
69−-- > AesonException "The key \"foo\" was not found"
70+-- > InvalidYaml (Just (YamlParseException {yamlProblem = "did not find expected ',' or '}'", yamlContext = "while parsing a flow mapping", yamlProblemMark = YamlMark {yamlIndex = 42, yamlLine = 2, yamlColumn = 12}})))
7071--
7172-- It looks more pleasant to print:
7273--
73−-- > Aeson exception: The key "foo" was not found
74+-- > YAML parse exception at line 2, column 12,
75+-- > while parsing a flow mapping:
76+-- > did not find expected ',' or '}'
7477--
7578-- Since 0.8.11
7679prettyPrintParseException :: ParseException -> String
77−prettyPrintParseException NonScalarKey = "Non scalar key"
78−prettyPrintParseException (UnknownAlias n) =
79− "Unknown alias: " ++ n
80−prettyPrintParseException (UnexpectedEvent r e) = unlines
81− [ "Unexpected event:"
82− , " Received: " ++ maybe "None" show r
83− , " Expected: " ++ maybe "None" show e
80+prettyPrintParseException pe = case pe of
81+ NonScalarKey -> "Non scalar key"
82+ UnknownAlias anchor -> "Unknown alias `" ++ anchor ++ "`"
83+ UnexpectedEvent mbExpected mbUnexpected -> unlines
84+ [ "Unexpected event: expected"
85+ , " " ++ show mbExpected
86+ , "but received"
87+ , " " ++ show mbUnexpected
8488 ]
85−prettyPrintParseException (InvalidYaml mye) =
86− case mye of
87− Just ye -> "Invalid yaml: " ++ show ye
88− _ -> "Invalid yaml"
89−prettyPrintParseException (AesonException e) =
90− "Aeson exception: " ++ e
91−prettyPrintParseException (OtherParseException e) =
92− "Parse exception: " ++ show e
93−prettyPrintParseException (NonStringKeyAlias n v) = unlines
94− [ "Non-string key alias:"
95− , " Anchor name: " ++ n
96− , " Value: " ++ show v
89+ InvalidYaml mbYamlError -> case mbYamlError of
90+ Nothing -> "Unspecified YAML error"
91+ Just yamlError -> case yamlError of
92+ YamlException s -> "YAML exception:\n" ++ s
93+ YamlParseException problem context mark -> unlines
94+ [ "YAML parse exception at line " ++ show (yamlLine mark) ++
95+ ", column " ++ show (yamlColumn mark) ++ ","
96+ -- The context seems to include a leading "while" or similar.
97+ , context ++ ":"
98+ , problem
99+ ]
100+ AesonException s -> "Aeson exception:\n" ++ s
101+ OtherParseException exc -> "Generic parse exception:\n" ++ show exc
102+ NonStringKeyAlias anchor value -> unlines
103+ [ "Non-string key alias:"
104+ , " Anchor name: " ++ anchor
105+ , " Value: " ++ show value
97106 ]
98−prettyPrintParseException CyclicIncludes = "Cyclic includes"
107+ CyclicIncludes -> "Cyclic includes"
99108
100109newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
101110instance Monad m => Functor (PErrorT m) where