Skip to content

Commit a91f9dd

Browse files
author
Chris Cornwell
committed
include key in error message from getField functions
1 parent 1e50939 commit a91f9dd

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/Data/Argonaut/Decode/Combinators.purs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Prelude
44

55
import Data.Argonaut.Core (Json)
66
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
7+
import Data.Bifunctor (lmap)
78
import Data.Either (Either(..))
89
import Data.Maybe (Maybe(..), fromMaybe, maybe)
910
import Foreign.Object as FO
@@ -12,7 +13,7 @@ getField :: forall a. DecodeJson a => FO.Object Json -> String -> Either String
1213
getField o s =
1314
maybe
1415
(Left $ "Expected field " <> show s)
15-
decodeJson
16+
(elaborateFailure s <<< decodeJson)
1617
(FO.lookup s o)
1718

1819
infix 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

2930
infix 7 getFieldOptional as .??
3031

3132
defaultField :: forall a. Either String (Maybe a) -> a -> Either String a
3233
defaultField parser default = fromMaybe default <$> parser
3334

3435
infix 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

0 commit comments

Comments
 (0)