Skip to content

Commit 0f7137d

Browse files
committed
error handling
1 parent 3392eff commit 0f7137d

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

ghcide/src/Development/IDE/Main.hs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import Control.Concurrent.Extra (newLock, readVar,
1111
withLock,
1212
withNumCapabilities)
1313
import Control.Exception.Safe (Exception (displayException),
14-
catchAny)
15-
import Control.Monad.Extra (concatMapM, unless,
16-
when)
14+
SomeException (SomeException),
15+
catch, catchAny)
16+
import Control.Monad.Extra (concatMapM, join,
17+
unless, when)
1718
import qualified Data.Aeson.Encode.Pretty as A
1819
import Data.Default (Default (def))
19-
import Data.Foldable (for_, traverse_)
20+
import Data.Foldable (traverse_)
2021
import qualified Data.HashMap.Strict as HashMap
2122
import Data.Hashable (hashed)
2223
import Data.List.Extra (intercalate, isPrefixOf,
@@ -255,8 +256,6 @@ defaultMain Arguments{..} = do
255256
PrintDefaultConfig ->
256257
LT.putStrLn $ decodeUtf8 $ A.encodePretty $ pluginsToDefaultConfig argsHlsPlugins
257258
LSP -> withNumCapabilities (maybe (numProcessors `div` 2) fromIntegral argsThreads) $ do
258-
server <- for argsMonitoringPort $ \p ->
259-
Monitoring.forkServer "localhost" (fromIntegral p)
260259
t <- offsetTime
261260
logInfo logger "Starting LSP server..."
262261
logInfo logger "If you are seeing this in a terminal, you probably should have run WITHOUT the --lsp option!"
@@ -293,6 +292,15 @@ defaultMain Arguments{..} = do
293292
hPutStrLn stderr $
294293
"Currently, HLS supports GHC 9 only partially. "
295294
<> "See [issue #297](https://github.com/haskell/haskell-language-server/issues/297) for more detail."
295+
296+
-- this can fail if the port is busy
297+
server <- fmap join $ for argsMonitoringPort $ \p ->
298+
(Just <$> Monitoring.forkServer "localhost" (fromIntegral p))
299+
`catch` \e@SomeException{} -> do
300+
logInfo logger $ T.pack $
301+
"Unable to bind monitoring server on port " <> show p <> ":" <> show e
302+
return Nothing
303+
296304
initialise
297305
argsDefaultHlsConfig
298306
rules

0 commit comments

Comments
 (0)