Skip to content

Commit

Permalink
Fix FromJSON instance for better error msgs (#1882)
Browse files Browse the repository at this point in the history
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"
```
  • Loading branch information
nitinprakash96 authored Jun 1, 2024
1 parent 5a034b5 commit 4a6f413
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/swarm-scenario/Swarm/Game/Entity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {..}
Expand Down

0 comments on commit 4a6f413

Please sign in to comment.