on master help sign in
Add parserThrowError and parserCatchError
commit4dc1aa8b1563616dddfc6da0324b4812972ad144 authorOleg Grenrus <oleg.grenrus@iki.fi> dateTue 6 Jun 2017, 14:57 (9 years ago) parent0ecde77
4 files changed+43−1
Data/Aeson/Types.hs+2 Data/Aeson/Types/Internal.hs+18−1 changelog.md+4 tests/Properties.hs+19
Data/Aeson/Types.hs+2
@@ -36,6 +36,8 @@ module Data.Aeson.Types
3636 , ToJSON(..)
3737 , KeyValue(..)
3838 , modifyFailure
39+ , parserThrowError
40+ , parserCatchError
3941
4042 -- ** Keys for maps
4143 , ToJSONKey(..)
Data/Aeson/Types/Internal.hs+18−1
@@ -44,6 +44,8 @@ module Data.Aeson.Types.Internal
4444 , parseEither
4545 , parseMaybe
4646 , modifyFailure
47+ , parserThrowError
48+ , parserCatchError
4749 , formatError
4850 , (<?>)
4951 -- * Constructors and accessors
@@ -507,7 +509,22 @@ p <?> pathElem = Parser $ \path kf ks -> runParser p (pathElem:path) kf ks
507509--
508510-- Since 0.6.2.0
509511modifyFailure :: (String -> String) -> Parser a -> Parser a
510−modifyFailure f (Parser p) = Parser $ \path kf ks -> p path (\p' m -> kf p' (f m)) ks
512+modifyFailure f (Parser p) = Parser $ \path kf ks ->
513+ p path (\p' m -> kf p' (f m)) ks
514+
515+-- | Throw a parser error with an additional path.
516+--
517+-- @since 1.2.1.0
518+parserThrowError :: JSONPath -> String -> Parser a
519+parserThrowError path' msg = Parser $ \path kf _ks ->
520+ kf (reverse path ++ path') msg
521+
522+-- | A handler function to handle previous errors and return to normal execution.
523+--
524+-- @since 1.2.1.0
525+parserCatchError :: Parser a -> (JSONPath -> String -> Parser a) -> Parser a
526+parserCatchError (Parser p) handler = Parser $ \path kf ks ->
527+ p path (\e msg -> runParser (handler e msg) path kf ks) ks
511528
512529--------------------------------------------------------------------------------
513530-- Generic and TH encoding configuration
changelog.md+4
@@ -1,5 +1,9 @@
11For the latest version of this document, please see [https://github.com/bos/aeson/blob/master/changelog.md](https://github.com/bos/aeson/blob/master/changelog.md).
22
3+## 1.2.1.0
4+
5+* Add `parserThrowError` and `parserCatchError` combinators.
6+
37## 1.2.0.0
48
59* `tagSingleConstructors`, an option to encode single-constructor types as tagged sums was added to `Options`. It is disabled by default for backward compatibility.
tests/Properties.hs+19
@@ -113,6 +113,23 @@ modifyFailureProp orig added =
113113 result :: Result ()
114114 result = parse parser ()
115115
116+parserThrowErrorProp :: String -> Property
117+parserThrowErrorProp msg =
118+ result === Error msg
119+ where
120+ parser = const $ parserThrowError [] msg
121+ result :: Result ()
122+ result = parse parser ()
123+
124+parserCatchErrorProp :: String -> Property
125+parserCatchErrorProp msg =
126+ result === Success msg
127+ where
128+ -- Fail, catch, use error message as a successful parse
129+ parser = const $ parserCatchError (fail msg) (\_ err -> pure err)
130+ result :: Result String
131+ result = parse parser ()
132+
116133-- | Perform a structural comparison of the results of two encoding
117134-- methods. Compares decoded values to account for HashMap-driven
118135-- variation in JSON object key ordering.
@@ -306,6 +323,8 @@ tests = testGroup "properties" [
306323 ]
307324 , testGroup "failure messages" [
308325 testProperty "modify failure" modifyFailureProp
326+ , testProperty "parserThrowError" parserThrowErrorProp
327+ , testProperty "parserCatchError" parserCatchErrorProp
309328 ]
310329 , testGroup "generic" [
311330 testGroup "toJSON" [