on master help sign in
Improve error messages when elm input file or binary can't be found
952c02fJustin Leitgeb
server/Server.hs+16−6
@@ -1,7 +1,7 @@
1
1
2module Main where
2module Main where
3
3
4−import Control.Monad (msum,guard)
4+import Control.Monad (msum,guard,when)
5import Control.Monad.Trans (MonadIO(liftIO))
5import Control.Monad.Trans (MonadIO(liftIO))
6import Data.List (isPrefixOf, isSuffixOf, (\\))
6import Data.List (isPrefixOf, isSuffixOf, (\\))
7import Data.Version (showVersion)
7import Data.Version (showVersion)
@@ -34,17 +34,23 @@ serve portNumber libLoc = do
34pageTitle :: String -> String
34pageTitle :: String -> String
35pageTitle = dropExtension . takeBaseName
35pageTitle = dropExtension . takeBaseName
36
36
37+defaultCacheDirectory = "elm-server-cache"
38+
37serveElm :: FilePath -> ServerPartT IO Response
39serveElm :: FilePath -> ServerPartT IO Response
38serveElm fp = do
40serveElm fp = do
41+ fileExists <- liftIO $ doesFileExist file
39− guard (takeExtension fp == ".elm")
42+ guard $ fileExists && takeExtension fp == ".elm"
40− let file = tail fp
43+
41− args = [ "--make" ,"--runtime=" ++ runtime,
42− "--cache-dir=elm-server-cache", file ]
43 (_, stdout, _, handle) <- liftIO $ createProcess $
44 (_, stdout, _, handle) <- liftIO $ createProcess $
44 (proc "elm" args) { std_out = CreatePipe }
45 (proc "elm" args) { std_out = CreatePipe }
45 exitCode <- liftIO $ waitForProcess handle
46 exitCode <- liftIO $ waitForProcess handle
47+
48+ dirExists <- liftIO $ doesDirectoryExist defaultCacheDirectory
46− liftIO $ removeDirectoryRecursive "elm-server-cache"
49+ when dirExists $ liftIO $ removeDirectoryRecursive defaultCacheDirectory
50+
47 case (exitCode, stdout) of
51 case (exitCode, stdout) of
52+ (ExitFailure 127, _) ->
53+ badRequest $ toResponse "Error: elm binary not found in your path."
48 (ExitFailure _, Just out) ->
54 (ExitFailure _, Just out) ->
49 do str <- liftIO $ hGetContents out
55 do str <- liftIO $ hGetContents out
50 badRequest $ toResponse str
56 badRequest $ toResponse str
@@ -54,6 +60,10 @@ serveElm fp = do
54 serveFile
60 serveFile
55 (asContentType "text/html") ("build" </> replaceExtension file "html")
61 (asContentType "text/html") ("build" </> replaceExtension file "html")
56
62
63+ where file = tail fp
64+ args = [ "--make" ,"--runtime=" ++ runtime,
65+ "--cache-dir=" ++ defaultCacheDirectory, file ]
66+
57serveLib :: FilePath -> [Char] -> ServerPartT IO Response
67serveLib :: FilePath -> [Char] -> ServerPartT IO Response
58serveLib libLoc fp = do
68serveLib libLoc fp = do
59 guard (fp == runtime)
69 guard (fp == runtime)