on master help sign in
decodeEither' for more useful exceptions #26
8b0b10eMichael Snoyman
Data/Yaml.hs+11
@@ -49,6 +49,7 @@ module Data.Yaml
49 , decodeFile
49 , decodeFile
50 -- ** Better error information
50 -- ** Better error information
51 , decodeEither
51 , decodeEither
52+ , decodeEither'
52 -- ** More control over decoding
53 -- ** More control over decoding
53 , decodeHelper
54 , decodeHelper
54 ) where
55 ) where
@@ -138,6 +139,7 @@ data ParseException = NonScalarKey
138 , _expected :: Maybe Event
139 , _expected :: Maybe Event
139 }
140 }
140 | InvalidYaml (Maybe YamlException)
141 | InvalidYaml (Maybe YamlException)
142+ | AesonException String
141 deriving (Show, Typeable)
143 deriving (Show, Typeable)
142instance Exception ParseException
144instance Exception ParseException
143
145
@@ -279,6 +281,15 @@ decodeEither bs = unsafePerformIO
279 $ fmap (either (Left . show) id)
281 $ fmap (either (Left . show) id)
280 $ decodeHelper (Y.decode bs)
282 $ decodeHelper (Y.decode bs)
281
283
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+
282decodeHelper :: FromJSON a
293decodeHelper :: FromJSON a
283 => C.Source Parse Y.Event
294 => C.Source Parse Y.Event
284 -> IO (Either ParseException (Either String a))
295 -> IO (Either ParseException (Either String a))
yaml.cabal+1−1
@@ -1,5 +1,5 @@
1name: yaml
1name: yaml
2−version: 0.8.2.4
2+version: 0.8.3
3license: BSD3
3license: BSD3
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
5author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov