2017-03-06 23:19:57 +00:00
|
|
|
-- ghc-mod: Happy Haskell Hacking
|
2015-03-03 19:28:34 +00:00
|
|
|
-- Copyright (C) 2015 Daniel Gröber <dxld ÄT darkboxed DOT org>
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published by
|
|
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
{-# LANGUAGE ExistentialQuantification #-}
|
2017-05-28 02:22:56 +00:00
|
|
|
module GhcMod.Error (
|
2014-08-28 09:54:01 +00:00
|
|
|
GhcModError(..)
|
2015-02-07 22:55:57 +00:00
|
|
|
, GmError
|
2014-09-18 08:05:47 +00:00
|
|
|
, gmeDoc
|
2015-03-03 20:12:43 +00:00
|
|
|
, ghcExceptionDoc
|
|
|
|
, liftMaybe
|
|
|
|
, overrideError
|
2014-08-28 09:54:01 +00:00
|
|
|
, modifyError
|
|
|
|
, modifyError'
|
2015-03-03 20:12:43 +00:00
|
|
|
, modifyGmError
|
2014-08-28 09:54:01 +00:00
|
|
|
, tryFix
|
2015-03-03 20:12:43 +00:00
|
|
|
, GHandler(..)
|
|
|
|
, gcatches
|
2014-08-28 09:54:01 +00:00
|
|
|
, module Control.Monad.Error
|
2015-03-03 20:12:43 +00:00
|
|
|
, module Control.Exception
|
2014-08-28 09:54:01 +00:00
|
|
|
) where
|
|
|
|
|
2015-09-14 07:42:45 +00:00
|
|
|
import Control.Arrow hiding ((<+>))
|
2015-03-03 20:12:43 +00:00
|
|
|
import Control.Exception
|
2015-03-04 20:48:21 +00:00
|
|
|
import Control.Monad.Error hiding (MonadIO, liftIO)
|
2015-03-03 20:12:43 +00:00
|
|
|
import qualified Data.Set as Set
|
2014-10-31 21:23:48 +00:00
|
|
|
import Data.List
|
2015-03-03 20:12:43 +00:00
|
|
|
import Data.Version
|
|
|
|
import System.Process (showCommandForUser)
|
|
|
|
import Text.Printf
|
2014-08-28 09:54:01 +00:00
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
import Exception
|
|
|
|
import Panic
|
2016-12-13 00:40:05 +00:00
|
|
|
import Pretty
|
2015-03-03 20:12:43 +00:00
|
|
|
import Config (cProjectVersion, cHostPlatformString)
|
2017-09-25 13:22:27 +00:00
|
|
|
import Paths_ghc_mod_core (version)
|
2015-02-07 22:55:57 +00:00
|
|
|
|
2017-05-28 02:22:56 +00:00
|
|
|
import GhcMod.Types
|
|
|
|
import GhcMod.Pretty
|
2015-02-07 22:55:57 +00:00
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
type GmError m = MonadError GhcModError m
|
2015-02-07 22:55:57 +00:00
|
|
|
|
2014-09-18 08:05:47 +00:00
|
|
|
gmeDoc :: GhcModError -> Doc
|
|
|
|
gmeDoc e = case e of
|
|
|
|
GMENoMsg ->
|
|
|
|
text "Unknown error"
|
|
|
|
GMEString msg ->
|
|
|
|
text msg
|
|
|
|
GMECabalConfigure msg ->
|
2015-09-15 03:25:29 +00:00
|
|
|
text "Configuring cabal project failed" <+>: gmeDoc msg
|
|
|
|
GMEStackConfigure msg ->
|
|
|
|
text "Configuring stack project failed" <+>: gmeDoc msg
|
|
|
|
GMEStackBootstrap msg ->
|
|
|
|
text "Bootstrapping stack project environment failed" <+>: gmeDoc msg
|
2015-03-03 20:12:43 +00:00
|
|
|
GMECabalCompAssignment ctx ->
|
|
|
|
text "Could not find a consistent component assignment for modules:" $$
|
|
|
|
(nest 4 $ foldr ($+$) empty $ map ctxDoc ctx) $$
|
2015-03-04 20:48:21 +00:00
|
|
|
text "" $$
|
2015-03-06 18:49:26 +00:00
|
|
|
(if all (Set.null . snd) ctx
|
|
|
|
then noComponentSuggestions
|
|
|
|
else empty) $$
|
2015-03-04 20:48:21 +00:00
|
|
|
text "- To find out which components ghc-mod knows about try:" $$
|
|
|
|
nest 4 (backticks $ text "ghc-mod debug")
|
2015-03-03 20:12:43 +00:00
|
|
|
|
|
|
|
where
|
2015-03-06 18:49:26 +00:00
|
|
|
noComponentSuggestions =
|
|
|
|
text "- Are some of these modules part of a test and or benchmark?\
|
|
|
|
\ Try enabling them:" $$
|
|
|
|
nest 4 (backticks $ text "cabal configure --enable-tests [--enable-benchmarks]")
|
|
|
|
|
2015-03-04 20:48:21 +00:00
|
|
|
backticks d = char '`' <> d <> char '`'
|
2015-03-03 20:12:43 +00:00
|
|
|
ctxDoc = moduleDoc *** compsDoc
|
|
|
|
>>> first (<> colon) >>> uncurry (flip hang 4)
|
|
|
|
|
|
|
|
moduleDoc (Left fn) =
|
|
|
|
text "File " <> quotes (text fn)
|
|
|
|
moduleDoc (Right mdl) =
|
|
|
|
text "Module " <> quotes (text $ moduleNameString mdl)
|
|
|
|
|
|
|
|
compsDoc sc | Set.null sc = text "has no known components"
|
|
|
|
compsDoc sc = fsep $ punctuate comma $
|
|
|
|
map gmComponentNameDoc $ Set.toList sc
|
2015-08-31 06:55:49 +00:00
|
|
|
GMEProcess _fn cmd args emsg -> let c = showCommandForUser cmd args in
|
2015-03-03 20:12:43 +00:00
|
|
|
case emsg of
|
|
|
|
Right err ->
|
|
|
|
text (printf "Launching system command `%s` failed: " c)
|
|
|
|
<> gmeDoc err
|
2015-08-31 06:55:49 +00:00
|
|
|
Left rv -> text $
|
2015-03-03 20:12:43 +00:00
|
|
|
printf "Launching system command `%s` failed (exited with %d)" c rv
|
2014-11-01 21:02:47 +00:00
|
|
|
GMENoCabalFile ->
|
|
|
|
text "No cabal file found."
|
|
|
|
GMETooManyCabalFiles cfs ->
|
|
|
|
text $ "Multiple cabal files found. Possible cabal files: \""
|
|
|
|
++ intercalate "\", \"" cfs ++"\"."
|
2015-02-07 22:55:57 +00:00
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
ghcExceptionDoc :: GhcException -> Doc
|
|
|
|
ghcExceptionDoc e@(CmdLineError _) =
|
|
|
|
text $ "<command line>: " ++ showGhcException e ""
|
|
|
|
ghcExceptionDoc (UsageError str) = strDoc str
|
|
|
|
ghcExceptionDoc (Panic msg) = vcat $ map text $ lines $ printf "\
|
|
|
|
\GHC panic! (the 'impossible' happened)\n\
|
|
|
|
\ ghc-mod version %s\n\
|
|
|
|
\ GHC library version %s for %s:\n\
|
|
|
|
\ %s\n\
|
|
|
|
\\n\
|
|
|
|
\Please report this as a bug: %s\n"
|
|
|
|
gmVer ghcVer platform msg url
|
|
|
|
where
|
|
|
|
gmVer = showVersion version
|
|
|
|
ghcVer = cProjectVersion
|
|
|
|
platform = cHostPlatformString
|
|
|
|
url = "https://github.com/kazu-yamamoto/ghc-mod/issues" :: String
|
|
|
|
|
|
|
|
ghcExceptionDoc e = text $ showGhcException e ""
|
|
|
|
|
|
|
|
liftMaybe :: MonadError e m => e -> m (Maybe a) -> m a
|
|
|
|
liftMaybe e action = maybe (throwError e) return =<< action
|
|
|
|
|
|
|
|
overrideError :: MonadError e m => e -> m a -> m a
|
|
|
|
overrideError e action = modifyError (const e) action
|
2014-09-18 08:05:47 +00:00
|
|
|
|
2014-08-28 09:54:01 +00:00
|
|
|
modifyError :: MonadError e m => (e -> e) -> m a -> m a
|
|
|
|
modifyError f action = action `catchError` \e -> throwError $ f e
|
|
|
|
|
|
|
|
infixr 0 `modifyError'`
|
|
|
|
modifyError' :: MonadError e m => m a -> (e -> e) -> m a
|
|
|
|
modifyError' = flip modifyError
|
|
|
|
|
2015-03-03 20:12:43 +00:00
|
|
|
modifyGmError :: (MonadIO m, ExceptionMonad m)
|
|
|
|
=> (GhcModError -> GhcModError) -> m a -> m a
|
|
|
|
modifyGmError f a = gcatch a $ \(ex :: GhcModError) -> liftIO $ throwIO (f ex)
|
|
|
|
|
2014-08-28 09:54:01 +00:00
|
|
|
tryFix :: MonadError e m => m a -> (e -> m ()) -> m a
|
2015-03-03 20:12:43 +00:00
|
|
|
tryFix action f = do
|
|
|
|
action `catchError` \e -> f e >> action
|
|
|
|
|
|
|
|
data GHandler m a = forall e . Exception e => GHandler (e -> m a)
|
|
|
|
|
2015-03-04 20:48:21 +00:00
|
|
|
gcatches :: (MonadIO m, ExceptionMonad m) => m a -> [GHandler m a] -> m a
|
2015-03-03 20:12:43 +00:00
|
|
|
gcatches io handlers = io `gcatch` gcatchesHandler handlers
|
|
|
|
|
2015-03-04 20:48:21 +00:00
|
|
|
gcatchesHandler :: (MonadIO m, ExceptionMonad m)
|
|
|
|
=> [GHandler m a] -> SomeException -> m a
|
2015-03-03 20:12:43 +00:00
|
|
|
gcatchesHandler handlers e = foldr tryHandler (liftIO $ throw e) handlers
|
|
|
|
where tryHandler (GHandler handler) res
|
|
|
|
= case fromException e of
|
|
|
|
Just e' -> handler e'
|
|
|
|
Nothing -> res
|