summaryrefslogtreecommitdiff
path: root/haskell/src/SpaceTraders/APIClient/Errors.hs
diff options
context:
space:
mode:
authorJulien Dessaux2023-07-09 14:31:31 +0200
committerJulien Dessaux2023-07-09 14:31:31 +0200
commit0f279a06d87c1bbe742ed102b60b86ef74807ad0 (patch)
treed3405b2cb90ebd32ade8e788ca82ae375acaf6f0 /haskell/src/SpaceTraders/APIClient/Errors.hs
parent[haskell] Simplify API Message JSON decoding (diff)
downloadspacetraders-0f279a06d87c1bbe742ed102b60b86ef74807ad0.tar.gz
spacetraders-0f279a06d87c1bbe742ed102b60b86ef74807ad0.tar.bz2
spacetraders-0f279a06d87c1bbe742ed102b60b86ef74807ad0.zip
[haskell] Simplify API Error JSON decoding
Diffstat (limited to '')
-rw-r--r--haskell/src/SpaceTraders/APIClient/Errors.hs17
1 files changed, 10 insertions, 7 deletions
diff --git a/haskell/src/SpaceTraders/APIClient/Errors.hs b/haskell/src/SpaceTraders/APIClient/Errors.hs
index 4ab47bc..e4e5513 100644
--- a/haskell/src/SpaceTraders/APIClient/Errors.hs
+++ b/haskell/src/SpaceTraders/APIClient/Errors.hs
@@ -11,17 +11,20 @@ import Data.Aeson
import Data.Time
import qualified Data.Text as T
-data APIError = APIError { apiErrorCode :: Int
- , apiErrorData :: Value
- , apiErrorMessage :: T.Text
- } deriving Show
+data APIError = APIError Int T.Text Value
+ | APIRateLimit RateLimit
+ deriving Show
instance Exception APIError
instance FromJSON APIError where
parseJSON (Object o) = do
e <- o .: "error"
- APIError <$> e .: "code"
- <*> e .: "data"
- <*> e .: "message"
+ code <- e .: "code"
+ d <- e .: "data"
+ case code of
+ 429 -> APIRateLimit <$> parseJSON d
+ _ -> APIError <$> pure code
+ <*> e .: "message"
+ <*> pure d
parseJSON _ = mzero
data RateLimit = RateLimit { limitBurst :: Int