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