Add withTimeout
[lsp-test.git] / src / Language / Haskell / LSP / Test / Exceptions.hs
index deea111f3e43006ce8084653b6e287b489950048..c8ca4f98e3512526fad50e30e18151cd24d2cf28 100644 (file)
@@ -1,10 +1,37 @@
 module Language.Haskell.LSP.Test.Exceptions where
 
 import Control.Exception
+import Language.Haskell.LSP.Messages
+import Language.Haskell.LSP.Types
+import Data.Aeson
+import qualified Data.ByteString.Lazy.Char8 as B
 
 data SessionException = TimeoutException
-  deriving Show
+                      | UnexpectedMessageException String FromServerMessage
+                      | ReplayOutOfOrderException FromServerMessage [FromServerMessage]
+                      | UnexpectedDiagnosticsException
+                      | IncorrectApplyEditRequestException String
+                      | UnexpectedResponseError LspIdRsp ResponseError
+  deriving Eq
+
 instance Exception SessionException
 
+instance Show SessionException where
+  show TimeoutException = "Timed out waiting to receive a message from the server."
+  show (UnexpectedMessageException expected lastMsg) =
+    "Received an unexpected message from the server:\n" ++
+    "Was parsing: " ++ expected ++ "\n" ++
+    "Last message received: " ++ show lastMsg
+  show (ReplayOutOfOrderException received expected) =
+    "Replay is out of order:\n" ++
+    -- Print json so its a bit easier to update the session logs
+    "Received from server:\n" ++ B.unpack (encode received) ++ "\n" ++
+    "Expected one of:\n" ++ unlines (map (B.unpack . encode) expected)
+  show UnexpectedDiagnosticsException = "Unexpectedly received diagnostics from the server."
+  show (IncorrectApplyEditRequestException msgStr) = "ApplyEditRequest didn't contain document, instead received:\n"
+                                          ++ msgStr
+  show (UnexpectedResponseError lid e) = "Received an exepected error in a response for id " ++ show lid ++ ":\n"
+                                          ++ show e
+
 anySessionException :: SessionException -> Bool
 anySessionException = const True