{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE StandaloneDeriving #-}
module RIO.PrettyPrint.PrettyException
( PrettyException (..)
, ppException
, prettyThrowIO
, prettyThrowM
) where
import RIO
( Exception (..), Maybe (..), MonadIO, MonadThrow, Show, SomeException
, Typeable, (.), throwIO, throwM
)
import Text.PrettyPrint.Leijen.Extended ( Pretty (..), StyleDoc, string )
data PrettyException
= forall e. (Exception e, Pretty e) => PrettyException e
deriving Typeable
deriving instance Show PrettyException
instance Pretty PrettyException where
pretty :: PrettyException -> StyleDoc
pretty (PrettyException e
e) = forall a. Pretty a => a -> StyleDoc
pretty e
e
instance Exception PrettyException where
displayException :: PrettyException -> String
displayException (PrettyException e
e) = forall e. Exception e => e -> String
displayException e
e
ppException :: SomeException -> StyleDoc
ppException :: SomeException -> StyleDoc
ppException SomeException
e = case forall e. Exception e => SomeException -> Maybe e
fromException SomeException
e of
Just (PrettyException e
e') -> forall a. Pretty a => a -> StyleDoc
pretty e
e'
Maybe PrettyException
Nothing -> (String -> StyleDoc
string forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. Exception e => e -> String
displayException) SomeException
e
prettyThrowIO :: (Exception e, MonadIO m, Pretty e) => e -> m a
prettyThrowIO :: forall e (m :: * -> *) a.
(Exception e, MonadIO m, Pretty e) =>
e -> m a
prettyThrowIO = forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. (Exception e, Pretty e) => e -> PrettyException
PrettyException
prettyThrowM :: (Exception e, MonadThrow m, Pretty e) => e -> m a
prettyThrowM :: forall e (m :: * -> *) a.
(Exception e, MonadThrow m, Pretty e) =>
e -> m a
prettyThrowM = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e. (Exception e, Pretty e) => e -> PrettyException
PrettyException