@@ -8,6 +8,7 @@ module Development.IDE.GHC.Error
8 , diagFromString
8 , diagFromString
9 , diagFromStrings
9 , diagFromStrings
10 , diagFromGhcException
10 , diagFromGhcException
12 -- * utilities working with spans
13 -- * utilities working with spans
13 , srcSpanToLocation
14 , srcSpanToLocation
@@ -23,6 +24,9 @@ import Development.IDE.GHC.Orphans()
23import qualified FastString as FS
24import qualified FastString as FS
26import ErrUtils
30import ErrUtils
27import SrcLoc
31import SrcLoc
28import qualified Outputable as Out
32import qualified Outputable as Out
@@ -111,6 +115,19 @@ realSpan = \case
111 UnhelpfulSpan _ -> Nothing
115 UnhelpfulSpan _ -> Nothing
118+-- | Run something in a Ghc monad and catch the errors (SourceErrors and
119+-- compiler-internal exceptions like Panic or InstallationError).
120+catchSrcErrors :: GhcMonad m => T.Text -> m a -> m (Either [FileDiagnostic] a)
121+catchSrcErrors fromWhere ghcM = do
122+ dflags <- getDynFlags
123+ handleGhcException (ghcExceptionToDiagnostics dflags) $
124+ handleSourceError (sourceErrorToDiagnostics dflags) $
127+ ghcExceptionToDiagnostics dflags = return . Left . diagFromGhcException fromWhere dflags
128+ sourceErrorToDiagnostics dflags = return . Left . diagFromErrMsgs fromWhere dflags . srcErrorMessages
114diagFromGhcException :: T.Text -> DynFlags -> GhcException -> [FileDiagnostic]
131diagFromGhcException :: T.Text -> DynFlags -> GhcException -> [FileDiagnostic]
115diagFromGhcException diagSource dflags exc = diagFromString diagSource (noSpan "<Internal>") (showGHCE dflags exc)
132diagFromGhcException diagSource dflags exc = diagFromString diagSource (noSpan "<Internal>") (showGHCE dflags exc)