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 = SourceError
srcErrorMessages :: SourceError -> ErrorMessages
srcErrorMessages (SourceError msgs) = msgs
throwErrors :: MonadIO io => ErrorMessages -> io a
throwErrors = liftIO . throwIO . mkSrcErr
throwOneError :: MonadIO io => MsgEnvelope DecoratedSDoc -> io a
throwOneError = throwErrors . unitBag
newtype SourceError = SourceError ErrorMessages
instance Show SourceError where
show (SourceError msgs) = unlines . map show . bagToList $ msgs
instance Exception SourceError
handleSourceError :: (MonadCatch m) =>
(SourceError -> m a)
-> m a
-> m a
handleSourceError handler act =
MC.catch act (\(e :: SourceError) -> handler e)