-- | 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 = 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

-- | 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 ([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

-- | 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 =
  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)