@@ -26,6 +26,7 @@ module Data.Aeson.Types.Internal
27 , parseEither
27 , parseEither
28 , parseMaybe
28 , parseMaybe
29 -- * Constructors and accessors
30 -- * Constructors and accessors
@@ -225,3 +226,18 @@ type Pair = (Text, Value)
225object :: [Pair] -> Value
226object :: [Pair] -> Value
226object = Object . H.fromList
227object = Object . H.fromList
227{-# INLINE object #-}
228{-# INLINE object #-}
230+-- | If the inner @Parser@ failed, modify the failure message using the
231+-- provided function. This allows you to create more meaningful error messages.
234+-- > parseJSON (Object o) = modifyFailure
235+-- > ("Parsing of the Foo value failed: " ++)
236+-- > (Foo <$> o .: "someField")
239+modifyFailure :: (String -> String) -> Parser a -> Parser a
240+modifyFailure f (Parser p) =
241+ Parser $ \failure success -> p (modifyFailure' failure) success
243+ modifyFailure' failure original = failure $ f original