Skip to content

Commit 12fb0d5

Browse files
committed
Fix error-throwing in initDebugger
Ever since "Fix terminate vs disconnect vs terminated", `throwError` of a `DebugAdaptor` computation does the "right thing" and aborts a Request/Response cycle by replying with a DAP `ErrorResponse`. We should use this mechanism directly rather than extraneous ExceptT wrapperes which ultimately do the same thing but in a more confusing way. Uniformise to use `throwError` to abort DAP computations!
1 parent 665a88a commit 12fb0d5

3 files changed

Lines changed: 15 additions & 36 deletions

File tree

hdb/Development/Debug/Adapter/Exit/Helpers.hs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import Data.Function
55
import System.IO
66
import Control.Monad
77
import Control.Monad.IO.Class
8-
import Control.Exception
9-
import Control.Exception.Context
108
import qualified Data.Text as T
119
import qualified Data.Text.IO as T
1210

@@ -47,12 +45,3 @@ terminateWithError msg = do
4745
destroyDebugSession
4846
sendTerminatedEvent (TerminatedEvent False)
4947
sendError (ErrorMessage (T.pack msg)) Nothing
50-
51-
--- Utils ----------------------------------------------------------------------
52-
53-
-- | Display an exception with its context
54-
displayExceptionWithContext :: SomeException -> String
55-
displayExceptionWithContext ex = do
56-
case displayExceptionContext (someExceptionContext ex) of
57-
"" -> displayException ex
58-
cx -> displayException ex ++ "\n\n" ++ cx

hdb/Development/Debug/Adapter/Init.hs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import qualified Data.Text as T
2121
import qualified Data.Text.IO as T
2222
import qualified Data.Text.Encoding as T
2323
import qualified System.Process as P
24+
import Control.Exception (displayExceptionWithInfo)
2425
import Control.Monad.Except
2526
import Control.Monad.Trans
2627
import Data.Function
@@ -40,7 +41,6 @@ import System.FilePath
4041
import Data.Functor.Contravariant
4142

4243
import Development.Debug.Adapter
43-
import Development.Debug.Adapter.Exit.Helpers
4444
import Colog.Core as Logger
4545
import qualified Development.Debug.Adapter.Output as Output
4646

@@ -95,15 +95,11 @@ data DAPLog
9595
-- * Launch Debugger
9696
--------------------------------------------------------------------------------
9797

98-
99-
-- | Exception type for when hie-bios initialization fails
100-
newtype InitFailed = InitFailed String deriving Show
101-
10298
-- | Initialize debugger
10399
--
104100
-- Returns @()@ if successful, throws @InitFailed@ otherwise
105101
initDebugger :: LogAction IO DAPLog -> Bool -> Bool
106-
-> LaunchArgs -> ExceptT InitFailed DebugAdaptor ()
102+
-> LaunchArgs -> DebugAdaptor ()
107103
initDebugger l supportsRunInTerminal preferInternalInterpreter
108104
LaunchArgs{ __sessionId
109105
, projectRoot = givenRoot
@@ -116,7 +112,7 @@ initDebugger l supportsRunInTerminal preferInternalInterpreter
116112
syncResponses <- liftIO newEmptyMVar
117113

118114
entryFile <- case entryFileMaybe of
119-
Nothing -> throwError $ InitFailed "Missing \"entryFile\" key in debugger configuration"
115+
Nothing -> throwError ("Missing \"entryFile\" key in debugger configuration", Nothing)
120116
Just ef -> pure ef
121117

122118
projectRoot <- maybe (liftIO getCurrentDirectory) pure givenRoot
@@ -142,8 +138,8 @@ initDebugger l supportsRunInTerminal preferInternalInterpreter
142138
| otherwise -> mempty
143139

144140
liftIO (runExceptT (hieBiosSetup hieBiosLogger projectRoot entryFile)) >>= \case
145-
Left e -> throwError $ InitFailed e
146-
Right (Left e) -> lift $ terminateWithError e
141+
Left e -> throwError (ErrorMessage (T.pack e), Nothing)
142+
Right (Left e) -> throwError (ErrorMessage (T.pack e), Nothing)
147143
Right (Right flags) -> do
148144

149145
let
@@ -177,7 +173,7 @@ initDebugger l supportsRunInTerminal preferInternalInterpreter
177173
dbgLog <- liftIO $
178174
createDebuggerLogger l dapLogger writeDAPOutput runInTerminalProc
179175

180-
(runInTerminalThreads, afterRegisterActions) <- lift $
176+
(runInTerminalThreads, afterRegisterActions) <-
181177
mkRunInTerminalThreads l runInTerminalProc preferInternalInterpreter
182178

183179
let
@@ -194,15 +190,15 @@ initDebugger l supportsRunInTerminal preferInternalInterpreter
194190
daState = DAS{entryFile=absEntryFile,..}
195191

196192
sessionId <- liftIO $ maybe (("debug-session:" <>) . T.show <$> UUID.nextRandom) (pure . T.pack) __sessionId
197-
lift $ registerNewDebugSession sessionId daState $
193+
registerNewDebugSession sessionId daState $
198194
[ debuggerThread dbgLog flags extraGhcArgs absEntryFile defaultRunConf syncRequests syncResponses
199195
, \withAdaptor -> forwardHandleToLogger readDAPOutput $
200196
LogAction (\msg -> withAdaptor (Output.neutral msg))
201197
]
202198
++
203199
runInTerminalThreads
204200

205-
lift afterRegisterActions
201+
afterRegisterActions
206202

207203
-- | Additional threads to register for this session depending on the process
208204
-- we're running through `runInTerminal` (see 'RunInTerminalProc').
@@ -299,7 +295,7 @@ debuggerThread l HieBiosFlags{..} extraGhcArgs mainFp runConf requests replies w
299295
req <- takeMVar requests & liftIO
300296
resp <- (Debugger.execute req <&> Right)
301297
`catch` \(e :: SomeException) -> do
302-
pure (Left (displayExceptionWithContext e))
298+
pure (Left (displayExceptionWithInfo e))
303299
case resp of
304300
Right x -> do
305301
liftIO (putMVar replies x)

hdb/Main.hs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Text.Read
1111
import Control.Concurrent
1212
import Control.Monad
1313
import Control.Monad.IO.Class
14-
import Control.Monad.Except
1514
import Control.Exception (bracket, uninterruptibleMask, bracketOnError)
1615
import Control.Exception.Backtrace
1716

@@ -283,19 +282,14 @@ talk l support_rit_var prefer_internal_interpreter = \ case
283282
-- Wrong-ish. See above where this variable is written
284283
supportsRunInTerminalRequest <- liftIO $ readIORef support_rit_var
285284

286-
merror <- runExceptT $
287-
initDebugger (contramap DAPLog l)
288-
supportsRunInTerminalRequest prefer_internal_interpreter
289-
launch_args
290-
case merror of
291-
Right () -> do
292-
sendLaunchResponse -- ack
293-
sendInitializedEvent -- our debugger is only ready to be configured after it has launched the session
285+
initDebugger (contramap DAPLog l)
286+
supportsRunInTerminalRequest prefer_internal_interpreter
287+
launch_args
294288

295-
liftLogIO l <& DAPLaunchLog (WithSeverity (T.pack "Debugger launched successfully.") Info)
289+
sendLaunchResponse -- ack
290+
sendInitializedEvent -- our debugger is only ready to be configured after it has launched the session
296291

297-
Left (InitFailed err) ->
298-
terminateWithError err
292+
liftLogIO l <& DAPLaunchLog (WithSeverity (T.pack "Debugger launched successfully.") Info)
299293
--------------------------------------------------------------------------------
300294
CommandAttach -> do
301295
sendTerminatedEvent (TerminatedEvent False)

0 commit comments

Comments
 (0)