Skip to content

Commit 9db32f5

Browse files
committed
Respond with error instead of silently doing nothing on errors
1 parent fb91e5f commit 9db32f5

3 files changed

Lines changed: 36 additions & 23 deletions

File tree

ext-common/Ext/Common.hs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,6 @@ cq_ bin args input = do
420420
pure $ (exit, stdOut, stdErr)
421421

422422

423-
execCombineStdOutErr :: String -> [String] -> String -> IO String
424-
execCombineStdOutErr bin args input = do
425-
(exit, stdOut, stdErr) <- c_ bin args input
426-
pure $ stdErr <> stdOut
427-
428-
429423
requireBinary :: String -> IO FilePath
430424
requireBinary name = do
431425
x <- Dir.findExecutable name

extra/Lamdera/CLI/Live.hs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import System.FilePath ((</>))
2626
import Control.Applicative ((<|>))
2727
import Control.Arrow ((***))
2828
import Control.Concurrent.STM (atomically, newTVarIO, readTVar, readTVarIO, writeTVar, TVar)
29-
import Control.Exception (finally)
29+
import Control.Exception (finally, try, SomeException)
3030
import qualified Language.Haskell.TH as TH
3131
import Data.FileEmbed (bsToExp)
3232
import qualified Data.Aeson.Encoding as A
@@ -57,6 +57,7 @@ import qualified Lamdera.CLI.Check
5757
import qualified Lamdera.Relative
5858
import qualified Lamdera.Version
5959
import 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

443444
tryOpenInDetectedEditor :: 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

461472
editors :: FilePath -> [IO (Maybe (B.Builder, EditorOpenIO))]
462473
editors 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+
738757
jsonResponse :: B.Builder -> Snap ()
739758
jsonResponse 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

extra/Lamdera/UiSourceMap.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,6 @@ url.pathname += node.fileName;
601601
url.searchParams.append("row", node.row);
602602
url.searchParams.append("column", node.column);
603603
fetch(url)
604-
.then(response => response.ok ? undefined : response.json().then(json => alert(json.error)))
604+
.then(response => response.ok ? undefined : response.text().then(message => alert(message)))
605605
.catch(error => alert(error.message));
606606
|]

0 commit comments

Comments
 (0)