{-# LANGUAGE DeriveDataTypeable #-}
module LDAP.Exceptions (
LDAPException(..),
catchLDAP,
handleLDAP,
failLDAP,
throwLDAP
)
where
import Data.Typeable
import Control.Exception
import LDAP.Types
import LDAP.Data
#if __GLASGOW_HASKELL__ < 610
import Data.Dynamic
#endif
data LDAPException = LDAPException
{LDAPException -> LDAPReturnCode
code :: LDAPReturnCode,
LDAPException -> String
description :: String,
LDAPException -> String
caller :: String
}
deriving (Typeable)
instance Show LDAPException where
show :: LDAPException -> String
show x :: LDAPException
x = LDAPException -> String
caller LDAPException
x String -> ShowS
forall a. [a] -> [a] -> [a]
++ ": LDAPException " String -> ShowS
forall a. [a] -> [a] -> [a]
++ LDAPReturnCode -> String
forall a. Show a => a -> String
show (LDAPException -> LDAPReturnCode
code LDAPException
x) String -> ShowS
forall a. [a] -> [a] -> [a]
++
"(" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (LDAPReturnCode -> Int
forall a. Enum a => a -> Int
fromEnum (LDAPReturnCode -> Int) -> LDAPReturnCode -> Int
forall a b. (a -> b) -> a -> b
$ LDAPException -> LDAPReturnCode
code LDAPException
x) String -> ShowS
forall a. [a] -> [a] -> [a]
++ "): " String -> ShowS
forall a. [a] -> [a] -> [a]
++
LDAPException -> String
description LDAPException
x
instance Eq LDAPException where
x :: LDAPException
x == :: LDAPException -> LDAPException -> Bool
== y :: LDAPException
y = LDAPException -> LDAPReturnCode
code LDAPException
x LDAPReturnCode -> LDAPReturnCode -> Bool
forall a. Eq a => a -> a -> Bool
== LDAPException -> LDAPReturnCode
code LDAPException
y
instance Ord LDAPException where
compare :: LDAPException -> LDAPException -> Ordering
compare x :: LDAPException
x y :: LDAPException
y = LDAPReturnCode -> LDAPReturnCode -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (LDAPException -> LDAPReturnCode
code LDAPException
x) (LDAPException -> LDAPReturnCode
code LDAPException
y)
#if __GLASGOW_HASKELL__ >= 610
instance Exception LDAPException where
catchLDAP :: IO a -> (LDAPException -> IO a) -> IO a
catchLDAP :: IO a -> (LDAPException -> IO a) -> IO a
catchLDAP action :: IO a
action handler :: LDAPException -> IO a
handler =
(LDAPException -> Maybe LDAPException)
-> IO a -> (LDAPException -> IO a) -> IO a
forall e b a.
Exception e =>
(e -> Maybe b) -> IO a -> (b -> IO a) -> IO a
catchJust LDAPException -> Maybe LDAPException
ldapExceptions IO a
action LDAPException -> IO a
handler
handleLDAP :: (LDAPException -> IO a) -> IO a -> IO a
handleLDAP :: (LDAPException -> IO a) -> IO a -> IO a
handleLDAP = (IO a -> (LDAPException -> IO a) -> IO a)
-> (LDAPException -> IO a) -> IO a -> IO a
forall a b c. (a -> b -> c) -> b -> a -> c
flip IO a -> (LDAPException -> IO a) -> IO a
forall a. IO a -> (LDAPException -> IO a) -> IO a
catchLDAP
ldapExceptions :: LDAPException -> Maybe LDAPException
ldapExceptions :: LDAPException -> Maybe LDAPException
ldapExceptions e :: LDAPException
e = LDAPException -> Maybe LDAPException
forall a. a -> Maybe a
Just LDAPException
e
#else
catchLDAP :: IO a -> (LDAPException -> IO a) -> IO a
catchLDAP = catchDyn
handleLDAP :: (LDAPException -> IO a) -> IO a -> IO a
handleLDAP = flip catchLDAP
#endif
failLDAP :: IO a -> IO a
failLDAP :: IO a -> IO a
failLDAP action :: IO a
action =
IO a -> (LDAPException -> IO a) -> IO a
forall a. IO a -> (LDAPException -> IO a) -> IO a
catchLDAP IO a
action LDAPException -> IO a
forall (m :: * -> *) a a. (MonadFail m, Show a) => a -> m a
handler
where handler :: a -> m a
handler e :: a
e = String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail ("LDAP error: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
e)
throwLDAP :: LDAPException -> IO a
#if __GLASGOW_HASKELL__ >= 610
throwLDAP :: LDAPException -> IO a
throwLDAP = LDAPException -> IO a
forall a e. Exception e => e -> a
throw
#else
throwLDAP = throwDyn
#endif