{-# LANGUAGE DeriveDataTypeable #-}

{- |
Module      : Network.MPD.Core.Error
Copyright   : (c) Joachim Fasting 2012
License     : MIT

Maintainer  : Joachim Fasting <joachifm@fastmail.fm>
Stability   : unstable
Portability : unportable

MPD errors.
-}

module Network.MPD.Core.Error where

import qualified Control.Exception as E
import           Data.Typeable

-- | The MPDError type is used to signal errors, both from the MPD and
-- otherwise.
data MPDError = NoMPD              -- ^ MPD not responding
              | ConnectionError E.IOException -- ^ An error occurred while talking to MPD.
              | Unexpected String  -- ^ MPD returned an unexpected response.
                                   --   This is a bug, either in the library or
                                   --   in MPD itself.
              | Custom String      -- ^ Used for misc. errors
              | ACK ACKType String -- ^ ACK type and a message from the
                                   --   server
                deriving (MPDError -> MPDError -> Bool
(MPDError -> MPDError -> Bool)
-> (MPDError -> MPDError -> Bool) -> Eq MPDError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MPDError -> MPDError -> Bool
== :: MPDError -> MPDError -> Bool
$c/= :: MPDError -> MPDError -> Bool
/= :: MPDError -> MPDError -> Bool
Eq, Typeable)

instance E.Exception MPDError

instance Show MPDError where
    show :: MPDError -> String
show MPDError
NoMPD          = String
"Could not connect to MPD"
    show (ConnectionError IOException
e) = String
"Connection error (" String -> ShowS
forall a. [a] -> [a] -> [a]
++ IOException -> String
forall a. Show a => a -> String
show IOException
e String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
    show (Unexpected String
s) = String
"MPD returned an unexpected response: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
unlines [
                            String
s
                          , String
""
                          , String
"This is most likely a bug in libmpd! Please report it here:"
                          , String
""
                          , String
"https://github.com/vimus/libmpd-haskell/issues/new"
                          ]
    show (Custom String
s)     = String
s
    show (ACK ACKType
_ String
s)      = String
s

-- | Represents various MPD errors (aka. ACKs).
data ACKType = InvalidArgument  -- ^ Invalid argument passed (ACK 2)
             | InvalidPassword  -- ^ Invalid password supplied (ACK 3)
             | Auth             -- ^ Authentication required (ACK 4)
             | UnknownCommand   -- ^ Unknown command (ACK 5)
             | FileNotFound     -- ^ File or directory not found ACK 50)
             | PlaylistMax      -- ^ Playlist at maximum size (ACK 51)
             | System           -- ^ A system error (ACK 52)
             | PlaylistLoad     -- ^ Playlist loading failed (ACK 53)
             | Busy             -- ^ Update already running (ACK 54)
             | NotPlaying       -- ^ An operation requiring playback
                                --   got interrupted (ACK 55)
             | FileExists       -- ^ File already exists (ACK 56)
             | UnknownACK       -- ^ An unknown ACK (aka. bug)
               deriving ACKType -> ACKType -> Bool
(ACKType -> ACKType -> Bool)
-> (ACKType -> ACKType -> Bool) -> Eq ACKType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ACKType -> ACKType -> Bool
== :: ACKType -> ACKType -> Bool
$c/= :: ACKType -> ACKType -> Bool
/= :: ACKType -> ACKType -> Bool
Eq