module GHC.Types.SourceError
( SourceError (..)
, mkSrcErr
, srcErrorMessages
, throwErrors
, throwOneError
, handleSourceError
)
where
import GHC.Prelude
import GHC.Data.Bag
import GHC.Types.Error
import GHC.Utils.Monad
import GHC.Utils.Panic
import GHC.Utils.Exception
import Control.Monad.Catch as MC (MonadCatch, catch)
mkSrcErr :: ErrorMessages -> SourceError
mkSrcErr :: ErrorMessages -> SourceError
mkSrcErr = ErrorMessages -> SourceError
SourceError
srcErrorMessages :: SourceError -> ErrorMessages
srcErrorMessages :: SourceError -> ErrorMessages
srcErrorMessages (SourceError ErrorMessages
msgs) = ErrorMessages
msgs
throwErrors :: MonadIO io => ErrorMessages -> io a
throwErrors :: forall (io :: * -> *) a. MonadIO io => ErrorMessages -> io a
throwErrors = IO a -> io a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> io a) -> (ErrorMessages -> IO a) -> ErrorMessages -> io a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceError -> IO a
forall e a. Exception e => e -> IO a
throwIO (SourceError -> IO a)
-> (ErrorMessages -> SourceError) -> ErrorMessages -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorMessages -> SourceError
mkSrcErr
throwOneError :: MonadIO io => MsgEnvelope DecoratedSDoc -> io a
throwOneError :: forall (io :: * -> *) a.
MonadIO io =>
MsgEnvelope DecoratedSDoc -> io a
throwOneError = ErrorMessages -> io a
forall (io :: * -> *) a. MonadIO io => ErrorMessages -> io a
throwErrors (ErrorMessages -> io a)
-> (MsgEnvelope DecoratedSDoc -> ErrorMessages)
-> MsgEnvelope DecoratedSDoc
-> io a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MsgEnvelope DecoratedSDoc -> ErrorMessages
forall a. a -> Bag a
unitBag
newtype SourceError = SourceError ErrorMessages
instance Show SourceError where
show :: SourceError -> String
show (SourceError ErrorMessages
msgs) = [String] -> String
unlines ([String] -> String)
-> (ErrorMessages -> [String]) -> ErrorMessages -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MsgEnvelope DecoratedSDoc -> String)
-> [MsgEnvelope DecoratedSDoc] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map MsgEnvelope DecoratedSDoc -> String
forall a. Show a => a -> String
show ([MsgEnvelope DecoratedSDoc] -> [String])
-> (ErrorMessages -> [MsgEnvelope DecoratedSDoc])
-> ErrorMessages
-> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorMessages -> [MsgEnvelope DecoratedSDoc]
forall a. Bag a -> [a]
bagToList (ErrorMessages -> String) -> ErrorMessages -> String
forall a b. (a -> b) -> a -> b
$ ErrorMessages
msgs
instance Exception SourceError
handleSourceError :: (MonadCatch m) =>
(SourceError -> m a)
-> m a
-> m a
handleSourceError :: forall (m :: * -> *) a.
MonadCatch m =>
(SourceError -> m a) -> m a -> m a
handleSourceError SourceError -> m a
handler m a
act =
m a -> (SourceError -> m a) -> m a
forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> (e -> m a) -> m a
MC.catch m a
act (\(SourceError
e :: SourceError) -> SourceError -> m a
handler SourceError
e)