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
4
4
5
5
import Data.Argonaut.Core (Json )
6
6
import Data.Argonaut.Decode.Class (class DecodeJson , decodeJson )
7
+ import Data.Bifunctor (lmap )
7
8
import Data.Either (Either (..))
8
9
import Data.Maybe (Maybe (..), fromMaybe , maybe )
9
10
import Foreign.Object as FO
@@ -12,7 +13,7 @@ getField :: forall a. DecodeJson a => FO.Object Json -> String -> Either String
12
13
getField o s =
13
14
maybe
14
15
(Left $ " Expected field " <> show s)
15
- decodeJson
16
+ (elaborateFailure s <<< decodeJson)
16
17
(FO .lookup s o)
17
18
18
19
infix 7 getField as .?
@@ -24,11 +25,17 @@ getFieldOptional o s =
24
25
decode
25
26
(FO .lookup s o)
26
27
where
27
- decode json = Just <$> decodeJson json
28
+ decode json = Just <$> (elaborateFailure s <<< decodeJson) json
28
29
29
30
infix 7 getFieldOptional as .??
30
31
31
32
defaultField :: forall a . Either String (Maybe a ) -> a -> Either String a
32
33
defaultField parser default = fromMaybe default <$> parser
33
34
34
35
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
You can’t perform that action at this time.
0 commit comments