Skip to content

Commit abae6ad

Browse files
committed
ResponseError takes LSPErrorCodes and ErrorCodes
1 parent 2c4243f commit abae6ad

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

lsp-test/test/DummyServer.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,6 @@ handlers =
241241
, requestHandler SMethod_TextDocumentSemanticTokensFull $ \_req resp -> do
242242
let tokens = makeSemanticTokens defaultSemanticTokensLegend [SemanticTokenAbsolute 0 1 2 SemanticTokenTypes_Type []]
243243
case tokens of
244-
Left t -> resp $ Left $ ResponseError ErrorCodes_InternalError t Nothing
244+
Left t -> resp $ Left $ ResponseError (InR ErrorCodes_InternalError) t Nothing
245245
Right tokens -> resp $ Right $ InL tokens
246246
]

lsp-types/src/Language/LSP/Protocol/Message/Types.hs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,23 @@ deriveJSON lspOptions ''RequestMessage
5454
-- | Response error type as defined in the spec.
5555
data ResponseError =
5656
ResponseError
57-
{ _code :: ErrorCodes
57+
{ _code :: LSPErrorCodes |? ErrorCodes
5858
, _message :: Text
5959
, _xdata :: Maybe Value
6060
} deriving stock (Show, Eq, Generic)
6161

62-
deriveJSON lspOptions ''ResponseError
62+
deriveToJSON lspOptions ''ResponseError
63+
instance FromJSON ResponseError where
64+
parseJSON =
65+
let errorCode = withObject "ResponseError" $ \v -> ResponseError
66+
<$> v .: "code"
67+
<*> v .: "message"
68+
<*> v .:? "data"
69+
in fmap go . errorCode
70+
where go :: ResponseError -> ResponseError
71+
go x@(ResponseError (InR (ErrorCodes_Custom n)) _ _) =
72+
x{_code = InL (fromOpenEnumBaseType n)}
73+
go x = x
6374

6475
-- | Response message type as defined in the spec.
6576
data ResponseMessage =
@@ -109,7 +120,7 @@ instance (ToJSON (MessageParams m)) => ToJSON (TRequestMessage m) where
109120

110121
data TResponseError (m :: Method f Request) =
111122
TResponseError
112-
{ _code :: ErrorCodes
123+
{ _code :: LSPErrorCodes |? ErrorCodes
113124
, _message :: Text
114125
, _xdata :: Maybe (ErrorData m)
115126
} deriving stock Generic
@@ -118,7 +129,16 @@ deriving stock instance Eq (ErrorData m) => Eq (TResponseError m)
118129
deriving stock instance Show (ErrorData m) => Show (TResponseError m)
119130

120131
instance (FromJSON (ErrorData m)) => FromJSON (TResponseError m) where
121-
parseJSON = genericParseJSON lspOptions
132+
parseJSON =
133+
let errorCode = withObject "ResponseError" $ \v -> TResponseError
134+
<$> v .: "code"
135+
<*> v .: "message"
136+
<*> v .:? "data"
137+
in fmap go . errorCode
138+
where go :: TResponseError m -> TResponseError m
139+
go x@(TResponseError (InR (ErrorCodes_Custom n)) _ _) =
140+
x{_code = InL (fromOpenEnumBaseType n)}
141+
go x = x
122142
instance (ToJSON (ErrorData m)) => ToJSON (TResponseError m) where
123143
toJSON = genericToJSON lspOptions
124144
toEncoding = genericToEncoding lspOptions

lsp-types/test/JsonSpec.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ instance Arbitrary ErrorCodes where
161161
, ErrorCodes_UnknownErrorCode
162162
]
163163

164+
instance Arbitrary LSPErrorCodes where
165+
arbitrary =
166+
elements
167+
[ LSPErrorCodes_RequestFailed
168+
, LSPErrorCodes_ServerCancelled
169+
, LSPErrorCodes_ContentModified
170+
, LSPErrorCodes_RequestCancelled
171+
]
164172
-- ---------------------------------------------------------------------
165173

166174
instance Arbitrary DidChangeWatchedFilesRegistrationOptions where

lsp/src/Language/LSP/Server/Processing.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ initializeRequestHandler ServerDefinition{..} vfs sendFunc req = do
165165

166166
initializeErrorHandler :: (ResponseError -> IO ()) -> E.SomeException -> IO (Maybe a)
167167
initializeErrorHandler sendResp e = do
168-
sendResp $ ResponseError ErrorCodes_InternalError msg Nothing
168+
sendResp $ ResponseError (InR ErrorCodes_InternalError) msg Nothing
169169
pure Nothing
170170
where
171171
msg = T.pack $ unwords ["Error on initialize:", show e]
@@ -370,7 +370,7 @@ handle' logger mAction m msg = do
370370
| SMethod_Shutdown <- m -> liftIO $ shutdownRequestHandler msg (mkRspCb msg)
371371
| otherwise -> do
372372
let errorMsg = T.pack $ unwords ["lsp:no handler for: ", show m]
373-
err = ResponseError ErrorCodes_MethodNotFound errorMsg Nothing
373+
err = ResponseError (InR ErrorCodes_MethodNotFound) errorMsg Nothing
374374
sendToClient $
375375
FromServerRsp (msg ^. J.method) $ TResponseMessage "2.0" (Just (msg ^. J.id)) (Left err)
376376

@@ -382,7 +382,7 @@ handle' logger mAction m msg = do
382382
Just h -> liftIO $ h req (mkRspCb req)
383383
Nothing -> do
384384
let errorMsg = T.pack $ unwords ["lsp:no handler for: ", show m]
385-
err = ResponseError ErrorCodes_MethodNotFound errorMsg Nothing
385+
err = ResponseError (InR ErrorCodes_MethodNotFound) errorMsg Nothing
386386
sendToClient $
387387
FromServerRsp (req ^. J.method) $ TResponseMessage "2.0" (Just (req ^. J.id)) (Left err)
388388
where

0 commit comments

Comments
 (0)