Print last received message in timeout exceptions
authorLuke Lau <luke_lau@icloud.com>
Tue, 26 Nov 2019 19:03:58 +0000 (19:03 +0000)
committerLuke Lau <luke_lau@icloud.com>
Tue, 26 Nov 2019 19:03:58 +0000 (19:03 +0000)
src/Language/Haskell/LSP/Test/Exceptions.hs
src/Language/Haskell/LSP/Test/Session.hs
test/Test.hs

index dd31ea3cc155d879ba5366966b04e9b4ca5a4808..88cf57d11ed6910391856ed85ffe9b244fcc5e26 100644 (file)
@@ -11,7 +11,7 @@ import Data.List
 import qualified Data.ByteString.Lazy.Char8 as B
 
 -- | An exception that can be thrown during a 'Haskell.LSP.Test.Session.Session'
-data SessionException = Timeout
+data SessionException = Timeout (Maybe FromServerMessage)
                       | NoContentLengthHeader
                       | UnexpectedMessage String FromServerMessage
                       | ReplayOutOfOrder FromServerMessage [FromServerMessage]
@@ -24,7 +24,11 @@ data SessionException = Timeout
 instance Exception SessionException
 
 instance Show SessionException where
-  show Timeout = "Timed out waiting to receive a message from the server."
+  show (Timeout lastMsg) =
+    "Timed out waiting to receive a message from the server." ++
+    case lastMsg of
+      Just msg -> "\nLast message received: " ++ show msg
+      Nothing -> mempty
   show NoContentLengthHeader = "Couldn't read Content-Length header from the server."
   show (UnexpectedMessage expected lastMsg) =
     "Received an unexpected message from the server:\n" ++
index 67e4ae65d7d44175d244f882190146030a309e3e..07b588666772f10a2369f92a37a5a96cb89aca67 100644 (file)
@@ -189,7 +189,7 @@ runSession context state (Session session) = runReaderT (runStateT conduit state
       curId <- curTimeoutId <$> get
       case msg of
         ServerMessage sMsg -> yield sMsg
-        TimeoutMessage tId -> when (curId == tId) $ throw Timeout
+        TimeoutMessage tId -> when (curId == tId) $ lastReceivedMessage <$> get >>= throw . Timeout
 
 -- | An internal version of 'runSession' that allows for a custom handler to listen to the server.
 -- It also does not automatically send initialize and exit messages.
@@ -290,7 +290,7 @@ updateState (ReqApplyWorkspaceEdit r) = do
           ctx <- ask
 
           -- if its not open, open it
-          unless (toNormalizedUri uri `Map.member` (vfsMap oldVFS)) $ do
+          unless (toNormalizedUri uri `Map.member` vfsMap oldVFS) $ do
             let fp = fromJust $ uriToFilePath uri
             contents <- liftIO $ T.readFile fp
             let item = TextDocumentItem (filePathToUri fp) "" 0 contents
index 342d889f464d73ce9435c56148bc428c5a7844ae..a0d4d0d4c210472ab2b62fdf4d6c356e6a5de917 100644 (file)
@@ -91,7 +91,9 @@ main = hspec $ do
                 getDocumentSymbols doc
                 -- should now timeout
                 skipManyTill anyMessage message :: Session ApplyWorkspaceEditRequest
-        in sesh `shouldThrow` (== Timeout)
+            isTimeout (Timeout _) = True
+            isTimeout _ = False
+        in sesh `shouldThrow` isTimeout
 
 
     describe "SessionException" $ do