module PayPal.Adaptive.Core.Error where
import Control.Exception
import Data.ByteString.Lazy (ByteString)
import Data.Text.Read
import qualified Data.Vector as V
import Import
data AdaptiveError
= AeConnectionError SomeException
| AeDecodeFailed ByteString Text
| AeErrorResponse ByteString ErrorResponse
deriving Show
data ErrorResponse = ErrorResponse { _unErrorResponse :: [PayError] } deriving (Eq, Show)
instance FromJSON ErrorResponse where
parseJSON = withObject "ErrorResponse" $ \o ->
ErrorResponse . V.toList <$> (traverse parseJSON =<< o .: "error")
data PayError = PayError { _errorCode :: ErrorCode
, _errorMessage :: Text
} deriving (Eq, Show)
instance FromJSON PayError where
parseJSON = withObject "PayError" $ \o -> PayError <$> o .: "errorId" <*> o .: "message"
newtype ErrorCode = ErrorCode { _unErrorCode :: Int } deriving Eq
instance Show ErrorCode where
show n | n == invalidCredentials = "Error code " <> show (_unErrorCode n) <> " - invalid credentials"
| n == noSuchEmail = "Error code " <> show (_unErrorCode n) <> " - no such email"
| otherwise = "Error code " <> show (_unErrorCode n)
instance FromJSON ErrorCode where
parseJSON = withText "ErrorCode" $ \t ->
case decimal t of
Left _ -> fail "Could not parse error response code to Int"
Right (b,_) -> return (ErrorCode b)
invalidCredentials :: ErrorCode
invalidCredentials = ErrorCode 520003
noSuchEmail :: ErrorCode
noSuchEmail = ErrorCode 589039
$(makeLenses ''ErrorCode)
$(makeLenses ''ErrorResponse)
$(makeLenses ''PayError)
$(makePrisms ''AdaptiveError)