From 16501021ea92ae3c84dbfd99a3c2dd631685b49c Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Tue, 26 Nov 2019 19:03:58 +0000 Subject: [PATCH] Print last received message in timeout exceptions --- src/Language/Haskell/LSP/Test/Exceptions.hs | 8 ++++++-- src/Language/Haskell/LSP/Test/Session.hs | 4 ++-- test/Test.hs | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Language/Haskell/LSP/Test/Exceptions.hs b/src/Language/Haskell/LSP/Test/Exceptions.hs index dd31ea3..88cf57d 100644 --- a/src/Language/Haskell/LSP/Test/Exceptions.hs +++ b/src/Language/Haskell/LSP/Test/Exceptions.hs @@ -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" ++ diff --git a/src/Language/Haskell/LSP/Test/Session.hs b/src/Language/Haskell/LSP/Test/Session.hs index 67e4ae6..07b5886 100644 --- a/src/Language/Haskell/LSP/Test/Session.hs +++ b/src/Language/Haskell/LSP/Test/Session.hs @@ -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 diff --git a/test/Test.hs b/test/Test.hs index 342d889..a0d4d0d 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -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 -- 2.30.2