on master help sign in
Provide a clearer message when the required tagKey is missing, #719
commit47604b6165260114bc3206798ca4f25e20e5103e authorgrdvnl <grdvnl@gmail.com> dateWed 18 Sep 2019, 05:05 (7 years ago) parent10681a5
1 file changed+8−1
Data/Aeson/Types/FromJSON.hs+8−1
Data/Aeson/Types/FromJSON.hs+8−1
@@ -664,6 +664,7 @@ instance (FromJSON a) => FromJSON [a] where
664664prependContext :: String -> Parser a -> Parser a
665665prependContext name = prependFailure ("parsing " ++ name ++ " failed, ")
666666
667+
667668-- | @'withObject' name f value@ applies @f@ to the 'Object' when @value@
668669-- is an 'Data.Aeson.Object' and fails otherwise.
669670--
@@ -910,6 +911,12 @@ type ConName = String
910911contextType :: TypeName -> Parser a -> Parser a
911912contextType = prependContext
912913
914+-- | Add the tagKey that will be looked up while building an ADT
915+contextTag :: Text -> Parser a -> Parser a
916+contextTag tagKey = prependFailure
917+ ("Missing required tagKey \"" ++ unpack tagKey ++
918+ "\". The parser returned error was: ")
919+
913920-- | Add the name of the constructor being parsed to a parser's error messages.
914921contextCons :: ConName -> TypeName -> Parser a -> Parser a
915922contextCons cname tname = prependContext (showCons cname tname)
@@ -1087,7 +1094,7 @@ parseNonAllNullarySum p@(tname :* opts :* _) =
10871094 case sumEncoding opts of
10881095 TaggedObject{..} ->
10891096 withObject tname $ \obj -> do
1090− tag <- contextType tname $ obj .: tagKey
1097+ tag <- (contextType tname) . (contextTag tagKey) $ obj .: tagKey
10911098 fromMaybe (badTag tag <?> Key tagKey) $
10921099 parseFromTaggedObject (tag :* contentsFieldName :* p) obj
10931100 where