Skip to content

Commit 047d832

Browse files
committed
catch exceptions in plugin Commands
1 parent a308151 commit 047d832

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Ide/Plugin.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Ide.Plugin
1818
, responseError
1919
) where
2020

21+
import Control.Exception(SomeException, catch)
2122
import Control.Lens ( (^.) )
2223
import Control.Monad
2324
import qualified Data.Aeson as J
@@ -206,7 +207,7 @@ executeCommandHandlers ecs = PartialHandlers $ \WithMessage{..} x -> return x{
206207
-- -> ExecuteCommandParams
207208
-- -> IO (Either ResponseError Value, Maybe (ServerMethod, ApplyWorkspaceEditParams))
208209
makeExecuteCommands :: [(PluginId, [PluginCommand])] -> LSP.LspFuncs Config -> ExecuteCommandProvider
209-
makeExecuteCommands ecs lf ide = do
210+
makeExecuteCommands ecs lf ide = wrapUnhandledExceptions $ do
210211
let
211212
pluginMap = Map.fromList ecs
212213
parseCmdId :: T.Text -> Maybe (PluginId, CommandId)
@@ -334,6 +335,14 @@ makeExecuteCommands ecs lf ide = do
334335

335336
-- | Runs a plugin command given a PluginId, CommandId and
336337
-- arguments in the form of a JSON object.
338+
wrapUnhandledExceptions ::
339+
(a -> IO (Either ResponseError J.Value, Maybe b)) ->
340+
a -> IO (Either ResponseError J.Value, Maybe b)
341+
wrapUnhandledExceptions action input =
342+
catch (action input) $ \(e::SomeException) -> do
343+
let resp = ResponseError InternalError (T.pack $ show e) Nothing
344+
return (Left resp, Nothing)
345+
337346
runPluginCommand :: Map.Map PluginId [PluginCommand]
338347
-> LSP.LspFuncs Config
339348
-> IdeState

0 commit comments

Comments
 (0)