on master help sign in
Merge pull request #290 from jsl/better_server_error_messages

Improve error messages when elm input file or binary can't be found

commit78242ef95be775b1e817c9067158bda76326b96c authorEvan Czaplicki <info@elm-lang.org> dateFri 11 Oct 2013, 20:04 (13 years ago) parentfb16adb10f13d1
1 file changed+30−13
server/Server.hs+30−13
server/Server.hs+30−13
@@ -1,7 +1,6 @@
1−
21module Main where
32
4−import Control.Monad (msum,guard)
3+import Control.Monad (msum,guard,when)
54import Control.Monad.Trans (MonadIO(liftIO))
65import Data.List (isPrefixOf, isSuffixOf, (\\))
76import Data.Version (showVersion)
@@ -34,24 +33,37 @@ serve portNumber libLoc = do
3433pageTitle :: String -> String
3534pageTitle = dropExtension . takeBaseName
3635
36+defaultCacheDirectory = "elm-server-cache"
37+
3738serveElm :: FilePath -> ServerPartT IO Response
3839serveElm fp = do
39− guard (takeExtension fp == ".elm")
40− let file = tail fp
41− args = [ "--make" ,"--runtime=" ++ runtime, "--cache-dir=elm-server-cache", file ]
42− (_, stdout, _, handle) <- liftIO $ createProcess $ (proc "elm" args) { std_out = CreatePipe }
40+ fileExists <- liftIO $ doesFileExist file
41+ guard $ fileExists && takeExtension fp == ".elm"
42+
43+ (_, stdout, _, handle) <- liftIO $ createProcess $
44+ (proc "elm" args) { std_out = CreatePipe }
4345 exitCode <- liftIO $ waitForProcess handle
44− liftIO $ removeDirectoryRecursive "elm-server-cache"
46+
47+ dirExists <- liftIO $ doesDirectoryExist defaultCacheDirectory
48+ when dirExists $ liftIO $ removeDirectoryRecursive defaultCacheDirectory
49+
4550 case (exitCode, stdout) of
51+ (ExitFailure 127, _) ->
52+ badRequest $ toResponse "Error: elm binary not found in your path."
4653 (ExitFailure _, Just out) ->
4754 do str <- liftIO $ hGetContents out
4855 badRequest $ toResponse str
4956 (ExitFailure _, Nothing) ->
5057 badRequest $ toResponse "See command line for error message."
5158 (ExitSuccess, _) ->
52− serveFile (asContentType "text/html") ("build" </> replaceExtension file "html")
59+ serveFile
60+ (asContentType "text/html") ("build" </> replaceExtension file "html")
61+
62+ where file = tail fp
63+ args = [ "--make" ,"--runtime=" ++ runtime,
64+ "--cache-dir=" ++ defaultCacheDirectory, file ]
5365
54−serveLib :: FilePath -> [Char] -> ServerPartT IO Response
66+serveLib :: FilePath -> String -> ServerPartT IO Response
5567serveLib libLoc fp = do
5668 guard (fp == runtime)
5769 serveFile (asContentType "application/javascript") libLoc
@@ -63,9 +75,11 @@ parse :: [String] -> IO ()
6375parse ("--help":_) = putStrLn usage
6476parse ("--version":_) = putStrLn ("The Elm Server " ++ showVersion version)
6577parse args =
66− case null remainingArgs of
67− True -> serve portNumber =<< elmRuntime
68− False -> putStrLn usageMini
78+ if null remainingArgs then
79+ serve portNumber =<< elmRuntime
80+ else
81+ putStrLn usageMini
82+
6983 where
7084 runtimeArg = filter (isPrefixOf "--runtime-location=") args
7185 portArg = filter (isPrefixOf "--port=") args
@@ -73,7 +87,10 @@ parse args =
7387
7488 argValue arg = tail $ dropWhile (/= '=') (head arg)
7589 portNumber = if null portArg then 8000 else read (argValue portArg) :: Int
76− elmRuntime = if null runtimeArg then Elm.runtime else return $ argValue runtimeArg
90+ elmRuntime = if null runtimeArg then
91+ Elm.runtime
92+ else
93+ return $ argValue runtimeArg
7794
7895usageMini :: String
7996usageMini =