{-# LANGUAGE OverloadedStrings #-} module Network.Lastfm.Error where import Control.Applicative ((<$>), empty) import Control.Monad ((<=<)) import Control.Monad.Error (Error) import Data.Aeson import Data.ByteString.Lazy.Char8 (ByteString) import Text.XML.Light data LastfmError = NotUsed -- To avoid subtracting 1 from Lastfm reponses | DoesntExist | InvalidService | InvalidMethod | AuthenticationFailed | InvalidFormat | InvalidParameters | InvalidResource | OperationFailed | InvalidSessionKey | InvalidAPIKey | ServiceOffline | SubscribersOnly | InvalidMethodSignature | TokenHasNotAuthorized | NotForStreaming | TemporaryUnavailable | LoginRequired | TrialExpired | DoesntExistAgain | NotEnoughContent | NotEnoughMembers | NotEnoughFans | NotEnoughNeighbours | NoPeakRadio | RadioNotFound | SuspendedAPIKey | Deprecated | RateLimitExceeded deriving Enum instance Show LastfmError where show NotUsed = "NotUsed: Internal liblastfm error for convenience" show DoesntExist = "DoesntExist: This error does not exist" show InvalidService = "InvalidService: This service does not exist" show InvalidMethod = "InvalidMethod: No method with that name in this package" show AuthenticationFailed = "AuthenticationFailed: You do not have permissions to access the service" show InvalidFormat = "InvalidFormat: This service doesn't exist in that format" show InvalidParameters = "InvalidParameters: Your request is missing a required parameter" show InvalidResource = "InvalidResource: Invalid resource specified" show OperationFailed = "OperationFailed: Something else went wrong" show InvalidSessionKey = "InvalidSessionKey: Please re-authenticate" show InvalidAPIKey = "InvalidAPIKey: You must be granted a valid key by last.fm" show ServiceOffline = "ServiceOffline: This service is temporarily offline. Try again later." show SubscribersOnly = "SubscribersOnly : This station is only available to paid last.fm subscribers" show InvalidMethodSignature = "InvalidMethodSignature: Invalid method signature supplied" show TokenHasNotAuthorized = "TokenHasNotAuthorized: This token has not been authorized" show NotForStreaming = "NotForStreaming: This item is not available for streaming." show TemporaryUnavailable = "TemporaryUnavailable: The service is temporarily unavailable, please try again." show LoginRequired = "LoginRequired: Login: User requires to be logged in" show TrialExpired = "TrialExpired: This user has no free radio plays left. Subscription required." show DoesntExistAgain = "DoesntExistAgain: This error does not exist" show NotEnoughContent = "NotEnoughContent: There is not enough content to play this station" show NotEnoughMembers = "NotEnoughMembers: This group does not have enough members for radio" show NotEnoughFans = "NotEnoughFans: This artist does not have enough fans for for radio" show NotEnoughNeighbours = "NotEnoughNeighbours: There are not enough neighbours for radio" show NoPeakRadio = "NoPeakRadio: This user is not allowed to listen to radio during peak usage" show RadioNotFound = "RadioNotFound: Radio station not found" show SuspendedAPIKey = "SuspendedAPIKey: Access for your account has been suspended, please contact Last.fm" show Deprecated = "Deprecated: This type of request is no longer supported" show RateLimitExceeded = "RateLimitExceeded: Your IP has made too many requests in a short period" instance Error LastfmError xmlError ∷ ByteString → Maybe LastfmError xmlError r = do xml ← parseXMLDoc r toEnum . read <$> (findAttr (unqual "code") <=< findChild (unqual "error")) xml jsonError ∷ ByteString → Maybe LastfmError jsonError = decode instance FromJSON LastfmError where parseJSON (Object v) = toEnum <$> v .: "error" parseJSON _ = empty