Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Prevent hie crash if apply-refact crashes #1220

Merged
merged 4 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Haskell/Ide/Engine/Plugin/ApplyRefact.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module Haskell.Ide.Engine.Plugin.ApplyRefact where

import Control.Arrow
import Control.Exception ( IOException
, ErrorCall
, Handler(..)
, catches
, try
)
import Control.Lens hiding ( List )
Expand Down Expand Up @@ -251,10 +254,17 @@ applyHint fp mhint fileMap = do
-- If we provide "applyRefactorings" with "Just (1,13)" then
-- the "Redundant bracket" hint will never be executed
-- because SrcSpan (1,20,??,??) doesn't contain position (1,13).
appliedFile <- liftIO $ applyRefactorings Nothing commands fp
diff <- ExceptT $ Right <$> makeDiffResult fp (T.pack appliedFile) fileMap
liftIO $ logm $ "applyHint:diff=" ++ show diff
return diff
res <- liftIO $ (Right <$> applyRefactorings Nothing commands fp) `catches`
[ Handler $ \e -> return (Left (show (e :: IOException)))
, Handler $ \e -> return (Left (show (e :: ErrorCall)))
]
case res of
Right appliedFile -> do
diff <- ExceptT $ Right <$> makeDiffResult fp (T.pack appliedFile) fileMap
liftIO $ logm $ "applyHint:diff=" ++ show diff
return diff
Left err ->
throwE (show err)

-- | Gets HLint ideas for
getIdeas :: MonadIO m => FilePath -> Maybe OneHint -> ExceptT String m [Idea]
Expand Down
2 changes: 2 additions & 0 deletions test/testdata/ApplyRefactError.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo :: forall a. (a -> a) -> a -> a
foo f x = f $ x
13 changes: 13 additions & 0 deletions test/unit/ApplyRefactPluginSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module ApplyRefactPluginSpec where

import qualified Data.HashMap.Strict as H
import qualified Data.Text as T
import Haskell.Ide.Engine.Plugin.ApplyRefact
import Haskell.Ide.Engine.MonadTypes
import Haskell.Ide.Engine.PluginUtils
Expand Down Expand Up @@ -153,3 +154,15 @@ applyRefactSpec = do
, _diagnostics = List []
}
))

-- ---------------------------------

it "reports error without crash" $ do
filePath <- filePathToUri <$> makeAbsolute "./test/testdata/ApplyRefactError.hs"

let req = applyAllCmd' filePath
isExpectedError (IdeResultFail (IdeError PluginError err _)) =
"Illegal symbol '.' in type" `T.isInfixOf` err
isExpectedError _ = False
r <- withCurrentDirectory "./test/testdata" $ runIGM testPlugins req
r `shouldSatisfy` isExpectedError