Skip to content
Browse files

Serve 500 with error message if compilation fails

  • Loading branch information...
1 parent 5d4c985 commit 4191e17f06d0819510463487617fa653d5a33074 @bergmark bergmark committed Aug 12, 2012
View
2 TODO
@@ -8,4 +8,4 @@
* Why does Paths_snaplet_fay produce a warning?
* TODO has to recompile if dependencies (imports) changed, should perhaps skip compileAll mode?
* Log messages should print relative paths
-* Compile errors should 500 and print compiler error
+* Option for not overwriting js files if they are newer than hs files (for debugging)
View
2 example/snaplets/fay/src/BrokenFile.hs
@@ -0,0 +1,2 @@
+
+broken:(
View
5 example/snaplets/heist/templates/index.tpl
@@ -12,6 +12,11 @@
<div>Current time:</div>
<div id="current-time"><current-time/></div>
<div><input id="current-time-button" type="button" value="Update current time"></div>
+
+ <br>
+
+ <div><a href="/fay/BrokenFile.js">See what happens when you try to load a file containing Syntax errors</a></div>
+ <br>
</div>
</body>
</html>
View
8 src/Snap/Snaplet/Fay.hs
@@ -85,11 +85,15 @@ compileWithMethod CompileOnDemand = do
uri <- (srcDir cfg </>) . toHsName . filename . BS.unpack . rqURI <$> getRequest
res <- liftIO (compileFile cfg uri)
case res of
- Just s -> writeLBS $ fromString s
- Nothing -> do
+ Success s -> writeLBS $ fromString s
+ NotFound -> do
modifyResponse $ setResponseStatus 404 "Not Found"
writeBS "File not found."
finishWith =<< getResponse
+ Error err -> do
+ modifyResponse $ setResponseStatus 500 "Internal Server Error"
+ writeBS . BS.pack $ err
+ finishWith =<< getResponse
compileWithMethod CompileAll = do
cfg <- get
View
14 src/Snap/Snaplet/Fay/Internal.hs
@@ -34,15 +34,17 @@ includeDirs = (:[]) . srcDir
data CompileMethod = CompileOnDemand | CompileAll
+data CompileResult = Success String | NotFound | Error String
+
-- | Compile a single file, print errors if they occur and return the
-- | compiled source if successful.
-compileFile :: Fay -> FilePath -> IO (Maybe String)
+compileFile :: Fay -> FilePath -> IO CompileResult
compileFile config f = do
exists <- doesFileExist f
if not exists
then do
putStrLn $ "snaplet-fay: Could not find: " ++ hsRelativePath f
- return Nothing
+ return NotFound
else do
res <- F.compileFile def { F.configDirectoryIncludes = includeDirs config
, F.configPrettyPrint = prettyPrint config
@@ -51,11 +53,11 @@ compileFile config f = do
Right out -> do
verbosePut config $ "Compiled " ++ hsRelativePath f
writeFile (jsPath config f) out
- return $ Just out
+ return $ Success out
Left err -> do
- putStrLn $ "snaplet-fay: Error compiling " ++ hsRelativePath f ++ ":"
- print err
- return Nothing
+ let errString = "snaplet-fay: Error compiling " ++ hsRelativePath f ++ ":\n" ++ show err
+ putStrLn errString
+ return $ Error errString
-- | Check if a file should be recompiled, either when the hs file was
-- | updated or the file hasn't been compiled at all.

0 comments on commit 4191e17

Please sign in to comment.
Something went wrong with that request. Please try again.