module Haxl.Core.Exception (
HaxlException(..),
InternalError(..),
internalErrorToException,
internalErrorFromException,
LogicError(..),
logicErrorToException,
logicErrorFromException,
TransientError(..),
transientErrorToException,
transientErrorFromException,
CriticalError(..),
DataSourceError(..),
NotFound(..),
UnexpectedType(..),
EmptyList(..),
JSONError(..),
InvalidParameter(..),
FetchError(..),
asHaxlException,
) where
import Control.Exception
import Data.Aeson
import Data.Typeable
import Data.Text (Text)
import Haxl.Core.Util
data HaxlException
= forall e. (Exception e, MiddleException e) => HaxlException e
deriving (Typeable)
instance Show HaxlException where
show (HaxlException e) = show e
instance Exception HaxlException
instance ToJSON HaxlException where
toJSON (HaxlException e) = object
[ "type" .= show (typeOf e)
, "name" .= eName e
, "txt" .= show e
]
haxlExceptionToException
:: (Exception e, MiddleException e) => e -> SomeException
haxlExceptionToException = toException . HaxlException
haxlExceptionFromException
:: (Exception e, MiddleException e) => SomeException -> Maybe e
haxlExceptionFromException x = do
HaxlException a <- fromException x
cast a
class (Exception a) => MiddleException a where
eName :: a -> String
data TransientError = forall e . (Exception e) => TransientError e
deriving (Typeable)
deriving instance Show TransientError
instance Exception TransientError where
toException = haxlExceptionToException
fromException = haxlExceptionFromException
instance MiddleException TransientError where
eName (TransientError e) = show $ typeOf e
transientErrorToException :: (Exception e) => e -> SomeException
transientErrorToException = toException . TransientError
transientErrorFromException
:: (Exception e) => SomeException -> Maybe e
transientErrorFromException x = do
TransientError a <- fromException x
cast a
data InternalError = forall e . (Exception e) => InternalError e
deriving (Typeable)
deriving instance Show InternalError
instance Exception InternalError where
toException = haxlExceptionToException
fromException = haxlExceptionFromException
instance MiddleException InternalError where
eName (InternalError e) = show $ typeOf e
internalErrorToException :: (Exception e) => e -> SomeException
internalErrorToException = toException . InternalError
internalErrorFromException
:: (Exception e) => SomeException -> Maybe e
internalErrorFromException x = do
InternalError a <- fromException x
cast a
data LogicError = forall e . (Exception e) => LogicError e
deriving (Typeable)
deriving instance Show LogicError
instance Exception LogicError where
toException = haxlExceptionToException
fromException = haxlExceptionFromException
instance MiddleException LogicError where
eName (LogicError e) = show $ typeOf e
logicErrorToException :: (Exception e) => e -> SomeException
logicErrorToException = toException . LogicError
logicErrorFromException
:: (Exception e) => SomeException -> Maybe e
logicErrorFromException x = do
LogicError a <- fromException x
cast a
data CriticalError = CriticalError Text
deriving (Typeable, Show)
instance Exception CriticalError where
toException = internalErrorToException
fromException = internalErrorFromException
data NotFound = NotFound Text
deriving (Typeable, Show)
instance Exception NotFound where
toException = logicErrorToException
fromException = logicErrorFromException
data UnexpectedType = UnexpectedType Text
deriving (Typeable, Show)
instance Exception UnexpectedType where
toException = logicErrorToException
fromException = logicErrorFromException
data EmptyList = EmptyList Text
deriving (Typeable,Show)
instance Exception EmptyList where
toException = logicErrorToException
fromException = logicErrorFromException
data JSONError = JSONError Text
deriving (Typeable, Show)
instance Exception JSONError where
toException = logicErrorToException
fromException = logicErrorFromException
data InvalidParameter = InvalidParameter Text
deriving (Typeable, Show)
instance Exception InvalidParameter where
toException = logicErrorToException
fromException = logicErrorFromException
data FetchError = FetchError Text
deriving (Typeable, Eq, Show)
instance Exception FetchError where
toException = transientErrorToException
fromException = transientErrorFromException
data DataSourceError = DataSourceError Text
deriving (Typeable, Eq, Show)
instance Exception DataSourceError where
toException = internalErrorToException
fromException = internalErrorFromException
asHaxlException :: SomeException -> HaxlException
asHaxlException e
| Just haxl_exception <- fromException e =
haxl_exception
| otherwise =
HaxlException (InternalError (CriticalError (textShow e)))