Fix FromJSON instance for better error msgs (#1882)

Before:
```
ghci> let s = "[{ \"radius\": 2, \"density\": \"x\"}]"
ghci> eitherDecodeStrict s :: Either String GrowthSpread
Left "Error in $: parsing Growth failed, expected Object, but encountered Array
```

The error msg above is misleading because we weren't trying to parse `Growth` datatype but `GrowthSpread` instead.

After:
```
ghci> let s = "[{ \"radius\": 2, \"density\": \"x\"}]"
ghci> eitherDecodeStrict s :: Either String GrowthSpread
Left "Error in $: parsing GrowthSpread failed, expected Object, but encountered Array"
```
This commit is contained in:
Nitin Prakash 2024-06-01 08:27:51 +05:30 committed by GitHub
parent 5a034b5939
commit 4a6f413c34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -192,7 +192,7 @@ data GrowthSpread = GrowthSpread
deriving (Eq, Ord, Show, Read, Generic, Hashable, ToJSON)
instance FromJSON GrowthSpread where
parseJSON = withObject "Growth" $ \v -> do
parseJSON = withObject "GrowthSpread" $ \v -> do
spreadRadius <- v .: "radius"
spreadDensity <- v .: "density"
pure GrowthSpread {..}