1 module Language.Haskell.LSP.Test.Exceptions where
3 import Control.Exception
4 import Language.Haskell.LSP.Messages
5 import Language.Haskell.LSP.Types
7 import Data.Aeson.Encode.Pretty
8 import Data.Algorithm.Diff
9 import Data.Algorithm.DiffOutput
11 import qualified Data.ByteString.Lazy.Char8 as B
13 -- | An exception that can be thrown during a 'Haskell.LSP.Test.Session.Session'
14 data SessionException = Timeout
15 | UnexpectedMessage String FromServerMessage
16 | ReplayOutOfOrder FromServerMessage [FromServerMessage]
17 | UnexpectedDiagnostics
18 | IncorrectApplyEditRequest String
19 | UnexpectedResponseError LspIdRsp ResponseError
20 | UnexpectedServerTermination
23 instance Exception SessionException
25 instance Show SessionException where
26 show Timeout = "Timed out waiting to receive a message from the server."
27 show (UnexpectedMessage expected lastMsg) =
28 "Received an unexpected message from the server:\n" ++
29 "Was parsing: " ++ expected ++ "\n" ++
30 "Last message received: " ++ show lastMsg
31 show (ReplayOutOfOrder received expected) =
32 let expected' = nub expected
33 getJsonDiff = lines . B.unpack . encodePretty
34 showExp exp = B.unpack (encodePretty exp) ++ "\nDiff:\n" ++
35 ppDiff (getGroupedDiff (getJsonDiff received) (getJsonDiff exp))
36 in "Replay is out of order:\n" ++
37 -- Print json so its a bit easier to update the session logs
38 "Received from server:\n" ++ B.unpack (encodePretty received) ++ "\n" ++
39 "Raw from server:\n" ++ B.unpack (encode received) ++ "\n" ++
40 "Expected one of:\n" ++ unlines (map showExp expected')
41 show UnexpectedDiagnostics = "Unexpectedly received diagnostics from the server."
42 show (IncorrectApplyEditRequest msgStr) = "ApplyEditRequest didn't contain document, instead received:\n"
44 show (UnexpectedResponseError lid e) = "Received an exepected error in a response for id " ++ show lid ++ ":\n"
46 show UnexpectedServerTermination = "Language server unexpectedly terminated"
48 -- | A predicate that matches on any 'SessionException'
49 anySessionException :: SessionException -> Bool
50 anySessionException = const True