File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import Prelude
44
55import Data.Argonaut.Core (Json )
66import Data.Argonaut.Decode.Class (class DecodeJson , decodeJson )
7+ import Data.Bifunctor (lmap )
78import Data.Either (Either (..))
89import Data.Maybe (Maybe (..), fromMaybe , maybe )
910import Foreign.Object as FO
@@ -12,7 +13,7 @@ getField :: forall a. DecodeJson a => FO.Object Json -> String -> Either String
1213getField o s =
1314 maybe
1415 (Left $ " Expected field " <> show s)
15- decodeJson
16+ (elaborateFailure s <<< decodeJson)
1617 (FO .lookup s o)
1718
1819infix 7 getField as .?
@@ -24,11 +25,17 @@ getFieldOptional o s =
2425 decode
2526 (FO .lookup s o)
2627 where
27- decode json = Just <$> decodeJson json
28+ decode json = Just <$> (elaborateFailure s <<< decodeJson) json
2829
2930infix 7 getFieldOptional as .??
3031
3132defaultField :: forall a . Either String (Maybe a ) -> a -> Either String a
3233defaultField parser default = fromMaybe default <$> parser
3334
3435infix 6 defaultField as .?=
36+
37+ elaborateFailure :: ∀ a . String -> Either String a -> Either String a
38+ elaborateFailure s e =
39+ lmap msg e
40+ where
41+ msg m = " Failed to decode key '" <> s <> " ': " <> m
You can’t perform that action at this time.
0 commit comments