2014-10-31 21:23:48 +00:00
|
|
|
{-# LANGUAGE TypeFamilies, ScopedTypeVariables, DeriveDataTypeable #-}
|
2014-08-28 09:54:01 +00:00
|
|
|
module Language.Haskell.GhcMod.Error (
|
|
|
|
GhcModError(..)
|
2014-09-18 08:05:47 +00:00
|
|
|
, gmeDoc
|
2014-08-28 09:54:01 +00:00
|
|
|
, modifyError
|
|
|
|
, modifyError'
|
|
|
|
, tryFix
|
|
|
|
, module Control.Monad.Error
|
|
|
|
, module Exception
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Monad.Error (MonadError(..), Error(..))
|
2014-10-31 21:23:48 +00:00
|
|
|
import Data.List
|
|
|
|
import Data.Typeable
|
2014-08-28 09:54:01 +00:00
|
|
|
import Exception
|
2014-09-18 08:05:47 +00:00
|
|
|
import Text.PrettyPrint
|
2014-08-28 09:54:01 +00:00
|
|
|
|
|
|
|
data GhcModError = GMENoMsg
|
|
|
|
-- ^ Unknown error
|
|
|
|
| GMEString String
|
|
|
|
-- ^ Some Error with a message. These are produced mostly by
|
|
|
|
-- 'fail' calls on GhcModT.
|
2014-10-31 21:23:48 +00:00
|
|
|
| GMEIOException IOException
|
|
|
|
-- ^ IOExceptions captured by GhcModT's MonadIO instance
|
2014-08-28 09:54:01 +00:00
|
|
|
| GMECabalConfigure GhcModError
|
|
|
|
-- ^ Configuring a cabal project failed.
|
2014-09-10 12:23:36 +00:00
|
|
|
| GMECabalFlags GhcModError
|
|
|
|
-- ^ Retrieval of the cabal configuration flags failed.
|
2014-08-28 09:54:01 +00:00
|
|
|
| GMEProcess [String] GhcModError
|
|
|
|
-- ^ Launching an operating system process failed. The first
|
|
|
|
-- field is the command.
|
2014-11-01 21:02:47 +00:00
|
|
|
| GMENoCabalFile
|
2014-11-02 20:43:49 +00:00
|
|
|
-- ^ No cabal file found.
|
2014-11-01 21:02:47 +00:00
|
|
|
| GMETooManyCabalFiles [FilePath]
|
2014-11-02 20:43:49 +00:00
|
|
|
-- ^ Too many cabal files found.
|
2014-10-31 21:23:48 +00:00
|
|
|
deriving (Eq,Show,Typeable)
|
|
|
|
|
|
|
|
instance Exception GhcModError
|
2014-08-28 09:54:01 +00:00
|
|
|
|
|
|
|
instance Error GhcModError where
|
|
|
|
noMsg = GMENoMsg
|
|
|
|
strMsg = GMEString
|
|
|
|
|
2014-09-18 08:05:47 +00:00
|
|
|
gmeDoc :: GhcModError -> Doc
|
|
|
|
gmeDoc e = case e of
|
|
|
|
GMENoMsg ->
|
|
|
|
text "Unknown error"
|
|
|
|
GMEString msg ->
|
|
|
|
text msg
|
2014-10-31 21:23:48 +00:00
|
|
|
GMEIOException ioe ->
|
|
|
|
text $ show ioe
|
2014-09-18 08:05:47 +00:00
|
|
|
GMECabalConfigure msg ->
|
|
|
|
text "cabal configure failed: " <> gmeDoc msg
|
|
|
|
GMECabalFlags msg ->
|
|
|
|
text "retrieval of the cabal configuration flags failed: " <> gmeDoc msg
|
|
|
|
GMEProcess cmd msg ->
|
|
|
|
text ("launching operating system process `"++unwords cmd++"` failed: ")
|
|
|
|
<> gmeDoc msg
|
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 ++"\"."
|
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
|
|
|
|
|
|
|
|
tryFix :: MonadError e m => m a -> (e -> m ()) -> m a
|
|
|
|
tryFix action fix = do
|
|
|
|
action `catchError` \e -> fix e >> action
|