@@ -26,6 +26,7 @@ module Data.Aeson.Types.Internal
2626 , parse
2727 , parseEither
2828 , parseMaybe
29+ , modifyFailure
2930 -- * Constructors and accessors
3031 , object
3132 ) where
@@ -225,3 +226,18 @@ type Pair = (Text, Value)
225226object :: [Pair] -> Value
226227object = Object . H.fromList
227228{-# INLINE object #-}
229+
230+-- | If the inner @Parser@ failed, modify the failure message using the
231+-- provided function. This allows you to create more meaningful error messages.
232+-- For example:
233+--
234+-- > parseJSON (Object o) = modifyFailure
235+-- > ("Parsing of the Foo value failed: " ++)
236+-- > (Foo <$> o .: "someField")
237+--
238+-- Since 0.6.2.0
239+modifyFailure :: (String -> String) -> Parser a -> Parser a
240+modifyFailure f (Parser p) =
241+ Parser $ \failure success -> p (modifyFailure' failure) success
242+ where
243+ modifyFailure' failure original = failure $ f original