module Database.Redis.IO.Types where
import Control.Exception (Exception, SomeException, catch)
import Data.IP
import Data.Typeable
import Network.Socket (SockAddr (..), PortNumber)
import System.Logger.Message
newtype Milliseconds = Ms { ms :: Int } deriving (Eq, Show, Num)
newtype InetAddr = InetAddr { sockAddr :: SockAddr } deriving (Eq, Ord)
instance Show InetAddr where
show (InetAddr (SockAddrInet p a)) =
let i = fromIntegral p :: Int in
shows (fromHostAddress a) . showString ":" . shows i $ ""
show (InetAddr (SockAddrInet6 p _ a _)) =
let i = fromIntegral p :: Int in
shows (fromHostAddress6 a) . showString ":" . shows i $ ""
show (InetAddr (SockAddrUnix unix)) = unix
#if MIN_VERSION_network(2,6,1)
show (InetAddr (SockAddrCan int32)) = show int32
#endif
instance ToBytes InetAddr where
bytes (InetAddr (SockAddrInet p a)) =
let i = fromIntegral p :: Int in
show (fromHostAddress a) +++ val ":" +++ i
bytes (InetAddr (SockAddrInet6 p _ a _)) =
let i = fromIntegral p :: Int in
show (fromHostAddress6 a) +++ val ":" +++ i
bytes (InetAddr (SockAddrUnix unix)) = bytes unix
#if MIN_VERSION_network(2,6,1)
bytes (InetAddr (SockAddrCan int32)) = bytes int32
#endif
ip2inet :: PortNumber -> IP -> InetAddr
ip2inet p (IPv4 a) = InetAddr $ SockAddrInet p (toHostAddress a)
ip2inet p (IPv6 a) = InetAddr $ SockAddrInet6 p 0 (toHostAddress6 a) 0
data ConnectionError
= ConnectionsBusy
| ConnectionClosed
| ConnectTimeout
deriving Typeable
instance Exception ConnectionError
instance Show ConnectionError where
show ConnectionsBusy = "redis-io: connections busy"
show ConnectionClosed = "redis-io: connection closed"
show ConnectTimeout = "redis-io: connect timeout"
newtype InternalError = InternalError String
deriving Typeable
instance Exception InternalError
instance Show InternalError where
show (InternalError e) = "redis-io: internal error: " ++ show e
newtype Timeout = Timeout String
deriving Typeable
instance Exception Timeout
instance Show Timeout where
show (Timeout e) = "redis-io: timeout: " ++ e
data TransactionFailure
= TransactionAborted
| TransactionDiscarded
| TransactionFailure String
deriving Typeable
instance Exception TransactionFailure
instance Show TransactionFailure where
show TransactionAborted = "redis-io: transaction aborted"
show TransactionDiscarded = "redis-io: transaction discarded"
show (TransactionFailure e) = "redis-io: transaction failed: " ++ e
ignore :: IO () -> IO ()
ignore a = catch a (const $ return () :: SomeException -> IO ())