@@ -13,6 +13,7 @@ import Control.Applicative (Const)
1313import Data.Aeson (eitherDecode, encode)
1414import Data.Aeson.Encoding (encodingToLazyByteString)
1515import Data.Aeson.Internal (IResult(..), formatError, ifromJSON, iparse)
16+import qualified Data.Aeson.Internal as I
1617import Data.Aeson.Parser (value)
1718import Data.Aeson.Types
1819import Data.DList (DList)
@@ -113,6 +114,29 @@ modifyFailureProp orig added =
113114 result :: Result ()
114115 result = parse parser ()
115116
117+parserThrowErrorProp :: String -> Property
118+parserThrowErrorProp msg =
119+ result === Error msg
120+ where
121+ parser = const $ parserThrowError [] msg
122+ result :: Result ()
123+ result = parse parser ()
124+
125+-- | Tests (also) that we catch the JSONPath and it has elements in the right order.
126+parserCatchErrorProp :: [String] -> String -> Property
127+parserCatchErrorProp path msg =
128+ result === Success ([I.Key "outer", I.Key "inner"] ++ jsonPath, msg)
129+ where
130+ parser = parserCatchError outer (curry pure)
131+
132+ outer = inner I.<?> I.Key "outer"
133+ inner = parserThrowError jsonPath msg I.<?> I.Key "inner"
134+
135+ result :: Result (I.JSONPath, String)
136+ result = parse (const parser) ()
137+
138+ jsonPath = map (I.Key . T.pack) path
139+
116140-- | Perform a structural comparison of the results of two encoding
117141-- methods. Compares decoded values to account for HashMap-driven
118142-- variation in JSON object key ordering.
@@ -306,6 +330,8 @@ tests = testGroup "properties" [
306330 ]
307331 , testGroup "failure messages" [
308332 testProperty "modify failure" modifyFailureProp
333+ , testProperty "parserThrowError" parserThrowErrorProp
334+ , testProperty "parserCatchError" parserCatchErrorProp
309335 ]
310336 , testGroup "generic" [
311337 testGroup "toJSON" [