From 0f279a06d87c1bbe742ed102b60b86ef74807ad0 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 9 Jul 2023 14:31:31 +0200 Subject: [haskell] Simplify API Error JSON decoding --- haskell/src/SpaceTraders/APIClient/Errors.hs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'haskell/src/SpaceTraders/APIClient/Errors.hs') 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 -- cgit v1.2.3