diff options
Diffstat (limited to '')
-rw-r--r-- | haskell/src/SpaceTraders/APIClient/Errors.hs | 17 |
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 |