Skip to content

Commit d511957

Browse files
committed
Catch and log errors in notification handlers
1 parent a774ba9 commit d511957

File tree

1 file changed

+16
-3
lines changed
  • ghcide/src/Development/IDE/Plugin

1 file changed

+16
-3
lines changed

ghcide/src/Development/IDE/Plugin/HLS.hs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,18 @@ import UnliftIO.Exception (catchAny)
5151
--
5252

5353
data Log
54-
= LogPluginError PluginId ResponseError
54+
= LogPluginError PluginId ResponseError
5555
| LogNoPluginForMethod (Some SMethod)
5656
| LogInvalidCommandIdentifier
57+
| ExceptionInPlugin PluginId (Some SMethod) SomeException
5758
instance Pretty Log where
5859
pretty = \case
5960
LogPluginError (PluginId pId) err -> pretty pId <> ":" <+> prettyResponseError err
6061
LogNoPluginForMethod (Some method) ->
6162
"No plugin enabled for " <> pretty (show method)
6263
LogInvalidCommandIdentifier-> "Invalid command identifier"
64+
ExceptionInPlugin plId (Some method) exception ->
65+
"Exception in plugin " <> viaShow plId <> " while processing "<> viaShow method <> ": " <> viaShow exception
6366

6467
instance Show Log where show = renderString . layoutCompact . pretty
6568

@@ -103,6 +106,13 @@ logAndReturnError recorder p errCode msg = do
103106
logWith recorder Warning $ LogPluginError p err
104107
pure $ Left err
105108

109+
-- | Logs the provider error before returning it to the caller
110+
logAndReturnError' :: Recorder (WithPriority Log) -> (LSPErrorCodes |? ErrorCodes) -> Log -> LSP.LspT Config IO (Either ResponseError a)
111+
logAndReturnError' recorder errCode msg = do
112+
let err = ResponseError errCode (fromString $ show msg) Nothing
113+
logWith recorder Warning $ msg
114+
pure $ Left err
115+
106116
-- | Map a set of plugins to the underlying ghcide engine.
107117
asGhcIdePlugin :: Recorder (WithPriority Log) -> IdePlugins IdeState -> Plugin Config
108118
asGhcIdePlugin recorder (IdePlugins ls) =
@@ -203,7 +213,7 @@ executeCommandHandlers recorder ecs = requestHandler SMethod_WorkspaceExecuteCom
203213
A.Error err -> logAndReturnError recorder p (InR ErrorCodes_InvalidParams) (failedToParseArgs com p err arg)
204214
A.Success a ->
205215
f ide a `catchAny`
206-
(\e -> pure $ Left $ ResponseError (InR ErrorCodes_InternalError) (exceptionInPlugin p SMethod_WorkspaceApplyEdit e) Nothing)
216+
(\e -> logAndReturnError' recorder (InR ErrorCodes_InternalError) (ExceptionInPlugin p (Some SMethod_WorkspaceApplyEdit) e))
207217

208218
-- ---------------------------------------------------------------------
209219

@@ -266,7 +276,10 @@ extensibleNotificationPlugins recorder xs = mempty { P.pluginHandlers = handlers
266276
Just fs -> do
267277
-- We run the notifications in order, so the core ghcide provider
268278
-- (which restarts the shake process) hopefully comes last
269-
mapM_ (\(pid,_,f) -> otTracedProvider pid (fromString $ show m) $ f ide vfs params) fs
279+
mapM_ (\(pid,_,f) -> otTracedProvider pid (fromString $ show m) $ f ide vfs params
280+
`catchAny`
281+
(\e -> logWith recorder Warning (ExceptionInPlugin pid (Some m) e))) fs
282+
270283

271284
-- ---------------------------------------------------------------------
272285

0 commit comments

Comments
 (0)