@@ -26,7 +26,7 @@ import System.FilePath ((</>))
2626import Control.Applicative ((<|>) )
2727import Control.Arrow ((***) )
2828import Control.Concurrent.STM (atomically , newTVarIO , readTVar , readTVarIO , writeTVar , TVar )
29- import Control.Exception (finally )
29+ import Control.Exception (finally , try , SomeException )
3030import qualified Language.Haskell.TH as TH
3131import Data.FileEmbed (bsToExp )
3232import qualified Data.Aeson.Encoding as A
@@ -57,6 +57,7 @@ import qualified Lamdera.CLI.Check
5757import qualified Lamdera.Relative
5858import qualified Lamdera.Version
5959import qualified Ext.Common
60+ import qualified GHC.IO.Exception
6061
6162
6263
@@ -325,7 +326,7 @@ openEditorHandler root = do
325326 serveEditorOpen root (FP. joinPath rest) row column
326327
327328 _ ->
328- error400 " Unexpected request, expecting format: /_x/editor/<filename>?row=<row>&column=<column>"
329+ error400PlainText " Unexpected request, expecting format: /_x/editor/<filename>?row=<row>&column=<column>"
329330
330331 _ ->
331332 pass
@@ -437,7 +438,7 @@ serveEditorOpen root path row column = do
437438 tryOpenInDetectedEditor root fullpath row column
438439
439440 else do
440- error404 " File not found"
441+ error400PlainText " File not found"
441442
442443
443444tryOpenInDetectedEditor :: FilePath -> FilePath -> Int -> Int -> Snap ()
@@ -451,48 +452,58 @@ tryOpenInDetectedEditor root file row column = do
451452 debug " 📝 found the following editors, opening first:"
452453 justs res & fmap fst & show & debug
453454
454- liftIO $ openEditor file row column
455- jsonResponse $ " { status: 'tried opening editor " <> editor <> " ' }"
455+ runRes <- liftIO (try (openEditor file row column) :: IO (Either SomeException (GHC.IO.Exception. ExitCode , String , String )))
456+ case runRes of
457+ Right (exit, stdout, stderr) ->
458+ case exit of
459+ GHC.IO.Exception. ExitSuccess ->
460+ noContentResponse
456461
462+ GHC.IO.Exception. ExitFailure exitCode ->
463+ error400PlainText $ Ext.Common. stringToBuilder $ " exit " <> show exitCode <> " : " <> stdout <> stderr
457464
458- type EditorOpenIO = (FilePath -> Int -> Int -> IO String )
465+ Left err ->
466+ error400PlainText $ Ext.Common. stringToBuilder $ show err
467+
468+
469+ type EditorOpenIO = (FilePath -> Int -> Int -> IO (GHC.IO.Exception. ExitCode , String , String ))
459470
460471
461472editors :: FilePath -> [IO (Maybe (B. Builder , EditorOpenIO ))]
462473editors projectRoot =
463474 [ detectEditor " custom-*nix"
464475 (Dir. doesFileExist (projectRoot </> " openEditor.sh" ))
465- (\ file row column -> Ext.Common. execCombineStdOutErr (projectRoot </> " openEditor.sh" ) [file, show row, show column] " " )
476+ (\ file row column -> Ext.Common. c_ (projectRoot </> " openEditor.sh" ) [file, show row, show column] " " )
466477
467478 , detectEditor " custom-windows"
468479 (do
469480 exists <- Dir. doesFileExist (projectRoot </> " openEditor.bat" )
470481 pure $ exists && ostype == Windows
471482 )
472- (\ file row column -> Ext.Common. execCombineStdOutErr (projectRoot </> " openEditor.bat" ) [file, show row, show column] " " )
483+ (\ file row column -> Ext.Common. c_ (projectRoot </> " openEditor.bat" ) [file, show row, show column] " " )
473484
474485 , detectExecutable " code-insiders"
475486 (\ executablePath file row column -> do
476- Ext.Common. execCombineStdOutErr executablePath [ " -g" , file <> " :" <> show row <> " :" <> show column] " "
487+ Ext.Common. c_ executablePath [ " -g" , file <> " :" <> show row <> " :" <> show column] " "
477488 )
478489
479490 , detectExecutable " code"
480491 (\ executablePath file row column -> do
481- Ext.Common. execCombineStdOutErr executablePath [ " -g" , file <> " :" <> show row <> " :" <> show column] " "
492+ Ext.Common. c_ executablePath [ " -g" , file <> " :" <> show row <> " :" <> show column] " "
482493 )
483494
484495 , detectEditor " intellij-ce"
485496 (Dir. doesDirectoryExist " /Applications/IntelliJ IDEA CE.app" )
486497 (\ file row column -> do
487498 -- IntelliJ seems to number it's columns from 1 index
488- Ext.Common. execCombineStdOutErr " open" [" -na" , " IntelliJ IDEA CE.app" , " --args" , " --line" , show row, " --column" , show (column - 1 ), file] " "
499+ Ext.Common. c_ " open" [" -na" , " IntelliJ IDEA CE.app" , " --args" , " --line" , show row, " --column" , show (column - 1 ), file] " "
489500 )
490501
491502 , detectEditor " intellij"
492503 (Dir. doesDirectoryExist " /Applications/IntelliJ IDEA.app" )
493504 (\ file row column -> do
494505 -- IntelliJ seems to number it's columns from 1 index
495- Ext.Common. execCombineStdOutErr " open" [" -na" , " IntelliJ IDEA.app" , " --args" , " --line" , show row, " --column" , show (column - 1 ), file] " "
506+ Ext.Common. c_ " open" [" -na" , " IntelliJ IDEA.app" , " --args" , " --line" , show row, " --column" , show (column - 1 ), file] " "
496507 )
497508 ]
498509
@@ -735,6 +746,14 @@ logger =
735746 atomicPutStrLn $ T. unpack $ TE. decodeUtf8 bs
736747 )
737748
749+
750+ noContentResponse :: Snap ()
751+ noContentResponse = do
752+ modifyResponse $ setResponseStatus 204 " No Content"
753+ r <- getResponse
754+ finishWith r
755+
756+
738757jsonResponse :: B. Builder -> Snap ()
739758jsonResponse s =
740759 do modifyResponse $ setContentType " application/json; charset=utf-8"
@@ -772,11 +791,11 @@ error503 s =
772791 r <- getResponse
773792 finishWith r
774793
775- error400 :: B. Builder -> Snap ()
776- error400 s =
794+ error400PlainText :: B. Builder -> Snap ()
795+ error400PlainText s =
777796 do modifyResponse $ setResponseStatus 400 " Bad Request"
778- modifyResponse $ setContentType " application/json ; charset=utf-8"
779- writeBuilder $ " { \" error \" : \" " <> s <> " \" } "
797+ modifyResponse $ setContentType " text/plain ; charset=utf-8"
798+ writeBuilder s
780799 r <- getResponse
781800 finishWith r
782801
0 commit comments