X-Git-Url: https://git.lukelau.me/?a=blobdiff_plain;f=src%2FLanguage%2FHaskell%2FLSP%2FTest%2FExceptions.hs;h=b28e256cc0857522c4558dd2d38e3823b6b3efbf;hb=84e2707604b3a64c00062104fa40e2ea76040155;hp=b337f0bee70032fc1b7595247966ca5a1ca6bedf;hpb=e2ae28cd825653b0cb8b982d113497e9ac795059;p=lsp-test.git diff --git a/src/Language/Haskell/LSP/Test/Exceptions.hs b/src/Language/Haskell/LSP/Test/Exceptions.hs index b337f0b..b28e256 100644 --- a/src/Language/Haskell/LSP/Test/Exceptions.hs +++ b/src/Language/Haskell/LSP/Test/Exceptions.hs @@ -1,37 +1,59 @@ module Language.Haskell.LSP.Test.Exceptions where import Control.Exception -import Language.Haskell.LSP.Messages import Language.Haskell.LSP.Types import Data.Aeson +import Data.Aeson.Encode.Pretty +import Data.Algorithm.Diff +import Data.Algorithm.DiffOutput +import Data.List import qualified Data.ByteString.Lazy.Char8 as B -data SessionException = Timeout +-- | An exception that can be thrown during a 'Haskell.LSP.Test.Session.Session' +data SessionException = Timeout (Maybe FromServerMessage) + | NoContentLengthHeader | UnexpectedMessage String FromServerMessage | ReplayOutOfOrder FromServerMessage [FromServerMessage] | UnexpectedDiagnostics | IncorrectApplyEditRequest String - | UnexpectedResponseError LspIdRsp ResponseError + | UnexpectedResponseError SomeLspId ResponseError + | UnexpectedServerTermination + | IllegalInitSequenceMessage FromServerMessage deriving Eq 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:\n" ++ B.unpack (encodePretty 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" ++ "Was parsing: " ++ expected ++ "\n" ++ - "Last message received: " ++ show lastMsg + "But the last message received was:\n" ++ B.unpack (encodePretty lastMsg) show (ReplayOutOfOrder received expected) = - "Replay is out of order:\n" ++ + let expected' = nub expected + getJsonDiff = lines . B.unpack . encodePretty + showExp exp = B.unpack (encodePretty exp) ++ "\nDiff:\n" ++ + ppDiff (getGroupedDiff (getJsonDiff received) (getJsonDiff exp)) + in "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) + "Received from server:\n" ++ B.unpack (encodePretty received) ++ "\n" ++ + "Raw from server:\n" ++ B.unpack (encode received) ++ "\n" ++ + "Expected one of:\n" ++ unlines (map showExp expected') show UnexpectedDiagnostics = "Unexpectedly received diagnostics from the server." show (IncorrectApplyEditRequest 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 + show UnexpectedServerTermination = "Language server unexpectedly terminated" + show (IllegalInitSequenceMessage msg) = + "Received an illegal message between the initialize request and response:\n" + ++ B.unpack (encodePretty msg) +-- | A predicate that matches on any 'SessionException' anySessionException :: SessionException -> Bool anySessionException = const True