on master help sign in
decodeEither' for more useful exceptions #26
commit8b0b10e9476c74476b06a3dafce164de4f36641d authorMichael Snoyman <michael@snoyman.com> dateTue 2 Jul 2013, 14:14 (13 years ago) parent10b12b3
2 files changed+12−1
Data/Yaml.hs+11 yaml.cabal+1−1
Data/Yaml.hs+11
@@ -49,6 +49,7 @@ module Data.Yaml
4949 , decodeFile
5050 -- ** Better error information
5151 , decodeEither
52+ , decodeEither'
5253 -- ** More control over decoding
5354 , decodeHelper
5455 ) where
@@ -138,6 +139,7 @@ data ParseException = NonScalarKey
138139 , _expected :: Maybe Event
139140 }
140141 | InvalidYaml (Maybe YamlException)
142+ | AesonException String
141143 deriving (Show, Typeable)
142144instance Exception ParseException
143145
@@ -279,6 +281,15 @@ decodeEither bs = unsafePerformIO
279281 $ fmap (either (Left . show) id)
280282 $ decodeHelper (Y.decode bs)
281283
284+-- | More helpful version of 'decodeEither' which returns the 'YamlException'.
285+--
286+-- Since 0.8.3
287+decodeEither' :: FromJSON a => ByteString -> Either ParseException a
288+decodeEither' = either Left (either (Left . AesonException) Right)
289+ . unsafePerformIO
290+ . decodeHelper
291+ . Y.decode
292+
282293decodeHelper :: FromJSON a
283294 => C.Source Parse Y.Event
284295 -> IO (Either ParseException (Either String a))
yaml.cabal+1−1
@@ -1,5 +1,5 @@
11name: yaml
2−version: 0.8.2.4
2+version: 0.8.3
33license: BSD3
44license-file: LICENSE
55author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov