-- | Source errors
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 = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Exception e => e -> IO a
throwIO 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 = forall (io :: * -> *) a. MonadIO io => ErrorMessages -> io a
throwErrors forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Bag a
unitBag

-- | A source error is an error that is caused by one or more errors in the
-- source code.  A 'SourceError' is thrown by many functions in the
-- compilation pipeline.  Inside GHC these errors are merely printed via
-- 'log_action', but API clients may treat them differently, for example,
-- insert them into a list box.  If you want the default behaviour, use the
-- idiom:
--
-- > handleSourceError printExceptionAndWarnings $ do
-- >   ... api calls that may fail ...
--
-- The 'SourceError's error messages can be accessed via 'srcErrorMessages'.
-- This list may be empty if the compiler failed due to @-Werror@
-- ('Opt_WarnIsError').
--
-- See 'printExceptionAndWarnings' for more information on what to take care
-- of when writing a custom error handler.
newtype SourceError = SourceError ErrorMessages

instance Show SourceError where
  show :: SourceError -> String
show (SourceError ErrorMessages
msgs) = [String] -> String
unlines forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Bag a -> [a]
bagToList forall a b. (a -> b) -> a -> b
$ ErrorMessages
msgs

instance Exception SourceError

-- | Perform the given action and call the exception handler if the action
-- throws a 'SourceError'.  See 'SourceError' for more information.
handleSourceError :: (MonadCatch m) =>
                     (SourceError -> m a) -- ^ exception handler
                  -> m a -- ^ action to perform
                  -> m a
handleSourceError :: forall (m :: * -> *) a.
MonadCatch m =>
(SourceError -> m a) -> m a -> m a
handleSourceError SourceError -> m a
handler m a
act =
  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)