Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print improvements #67

Merged
merged 4 commits into from
Aug 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Data/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ decodeFileEither = decodeHelper_ . Y.decodeFile

decodeEither :: FromJSON a => ByteString -> Either String a
decodeEither bs = unsafePerformIO
$ fmap (either (Left . show) id)
$ fmap (either (Left . prettyPrintParseException) id)
$ decodeHelper (Y.decode bs)

-- | More helpful version of 'decodeEither' which returns the 'YamlException'.
Expand Down
53 changes: 31 additions & 22 deletions Data/Yaml/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Data.Aeson.Types hiding (parse)
import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
import Data.ByteString (ByteString)
import qualified Data.Map as Map
import Data.Maybe (isNothing)
import Control.Exception
import Control.Exception.Enclosed
import Control.Monad.Trans.State
Expand Down Expand Up @@ -66,36 +67,44 @@ instance Exception ParseException where
-- Instead of displaying the data constructors applied to their arguments,
-- a more textual output is returned. For example, instead of printing:
--
-- > AesonException "The key \"foo\" was not found"
-- > InvalidYaml (Just (YamlParseException {yamlProblem = "did not find expected ',' or '}'", yamlContext = "while parsing a flow mapping", yamlProblemMark = YamlMark {yamlIndex = 42, yamlLine = 2, yamlColumn = 12}})))
--
-- It looks more pleasant to print:
--
-- > Aeson exception: The key "foo" was not found
-- > YAML parse exception at line 2, column 12,
-- > while parsing a flow mapping:
-- > did not find expected ',' or '}'
--
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an actual change in behavior here, or is it just restructuring the code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is, the part that I care most about is nice reporting when there is a YamlError, but I also report in a friendlier way when there is an UnexpectedEvent.

-- Since 0.8.11
prettyPrintParseException :: ParseException -> String
prettyPrintParseException NonScalarKey = "Non scalar key"
prettyPrintParseException (UnknownAlias n) =
"Unknown alias: " ++ n
prettyPrintParseException (UnexpectedEvent r e) = unlines
[ "Unexpected event:"
, " Received: " ++ maybe "None" show r
, " Expected: " ++ maybe "None" show e
prettyPrintParseException pe = case pe of
NonScalarKey -> "Non scalar key"
UnknownAlias anchor -> "Unknown alias `" ++ anchor ++ "`"
UnexpectedEvent mbExpected mbUnexpected -> unlines
[ "Unexpected event: expected"
, " " ++ show mbExpected
, "but received"
, " " ++ show mbUnexpected
]
prettyPrintParseException (InvalidYaml mye) =
case mye of
Just ye -> "Invalid yaml: " ++ show ye
_ -> "Invalid yaml"
prettyPrintParseException (AesonException e) =
"Aeson exception: " ++ e
prettyPrintParseException (OtherParseException e) =
"Parse exception: " ++ show e
prettyPrintParseException (NonStringKeyAlias n v) = unlines
[ "Non-string key alias:"
, " Anchor name: " ++ n
, " Value: " ++ show v
InvalidYaml mbYamlError -> case mbYamlError of
Nothing -> "Unspecified YAML error"
Just yamlError -> case yamlError of
YamlException s -> "YAML exception:\n" ++ s
YamlParseException problem context mark -> unlines
[ "YAML parse exception at line " ++ show (yamlLine mark) ++
", column " ++ show (yamlColumn mark) ++ ","
-- The context seems to include a leading "while" or similar.
, context ++ ":"
, problem
]
AesonException s -> "Aeson exception:\n" ++ s
OtherParseException exc -> "Generic parse exception:\n" ++ show exc
NonStringKeyAlias anchor value -> unlines
[ "Non-string key alias:"
, " Anchor name: " ++ anchor
, " Value: " ++ show value
]
prettyPrintParseException CyclicIncludes = "Cyclic includes"
CyclicIncludes -> "Cyclic includes"

newtype PErrorT m a = PErrorT { runPErrorT :: m (Either ParseException a) }
instance Monad m => Functor (PErrorT m) where
Expand Down