on master help sign in
Improve error messages when elm input file or binary can't be found

There are a few problems currently with the elm server: 1. When the elm binary is not found, a nondescript error is displayed 2. When a file has a suffix .elm but the file is not found, the same error as in (1) is displayed 3. When a non-existing file ending in .elm is requested, the error code is incorrect - the elm-server returns a 400 instead of a 404 The following is the error message that shows up in cases (1) and (2) above: Server error: elm-server-cache: getDirectoryContents: does not exist (No such file or directory) This commit fixes the generic runtime errors (source of (1) and (2) above) by guarding against trying to delete a non-existing directory. It also fixes the HTTP code for non-existing .elm files by returning the general Happstack 404 page that is returned for other not-found requests.

commit952c02fabf498eda404dcf8e7a235e4e744d02a8 authorJustin Leitgeb <justin@stackbuilders.com> dateFri 11 Oct 2013, 14:51 (13 years ago) parent05dbca5
1 file changed+16−6
server/Server.hs+16−6
server/Server.hs+16−6
@@ -1,7 +1,7 @@
11
22module Main where
33
4−import Control.Monad (msum,guard)
4+import Control.Monad (msum,guard,when)
55import Control.Monad.Trans (MonadIO(liftIO))
66import Data.List (isPrefixOf, isSuffixOf, (\\))
77import Data.Version (showVersion)
@@ -34,17 +34,23 @@ serve portNumber libLoc = do
3434pageTitle :: String -> String
3535pageTitle = dropExtension . takeBaseName
3636
37+defaultCacheDirectory = "elm-server-cache"
38+
3739serveElm :: FilePath -> ServerPartT IO Response
3840serveElm fp = do
39− guard (takeExtension fp == ".elm")
40− let file = tail fp
41− args = [ "--make" ,"--runtime=" ++ runtime,
42− "--cache-dir=elm-server-cache", file ]
41+ fileExists <- liftIO $ doesFileExist file
42+ guard $ fileExists && takeExtension fp == ".elm"
43+
4344 (_, stdout, _, handle) <- liftIO $ createProcess $
4445 (proc "elm" args) { std_out = CreatePipe }
4546 exitCode <- liftIO $ waitForProcess handle
46− liftIO $ removeDirectoryRecursive "elm-server-cache"
47+
48+ dirExists <- liftIO $ doesDirectoryExist defaultCacheDirectory
49+ when dirExists $ liftIO $ removeDirectoryRecursive defaultCacheDirectory
50+
4751 case (exitCode, stdout) of
52+ (ExitFailure 127, _) ->
53+ badRequest $ toResponse "Error: elm binary not found in your path."
4854 (ExitFailure _, Just out) ->
4955 do str <- liftIO $ hGetContents out
5056 badRequest $ toResponse str
@@ -54,6 +60,10 @@ serveElm fp = do
5460 serveFile
5561 (asContentType "text/html") ("build" </> replaceExtension file "html")
5662
63+ where file = tail fp
64+ args = [ "--make" ,"--runtime=" ++ runtime,
65+ "--cache-dir=" ++ defaultCacheDirectory, file ]
66+
5767serveLib :: FilePath -> [Char] -> ServerPartT IO Response
5868serveLib libLoc fp = do
5969 guard (fp == runtime)