forked from GitHub/gf-core
PGF Service: return error code 404 (not found) instead of 500 (internal server error) for missing grammars
This commit is contained in:
@@ -37,6 +37,7 @@ import System.Random
|
|||||||
import System.Process
|
import System.Process
|
||||||
import System.Exit
|
import System.Exit
|
||||||
import System.IO
|
import System.IO
|
||||||
|
import System.IO.Error(isDoesNotExistError)
|
||||||
import System.Directory(removeFile)
|
import System.Directory(removeFile)
|
||||||
import System.Mem(performGC)
|
import System.Mem(performGC)
|
||||||
import Fold(fold) -- transfer function for OpenMath LaTeX
|
import Fold(fold) -- transfer function for OpenMath LaTeX
|
||||||
@@ -82,14 +83,21 @@ cgiMain' cache path =
|
|||||||
do command <- liftM (maybe "grammar" (urlDecodeUnicode . UTF8.decodeString))
|
do command <- liftM (maybe "grammar" (urlDecodeUnicode . UTF8.decodeString))
|
||||||
(getInput "command")
|
(getInput "command")
|
||||||
case command of
|
case command of
|
||||||
"download" -> outputBinary =<< liftIO (BS.readFile path)
|
"download" -> outputBinary =<< getFile BS.readFile path
|
||||||
'c':'-':_ ->
|
'c':'-':_ ->
|
||||||
#ifdef C_RUNTIME
|
#ifdef C_RUNTIME
|
||||||
cpgfMain command =<< liftIO (readCache' (snd cache) path)
|
cpgfMain command =<< getFile (readCache' (snd cache)) path
|
||||||
#else
|
#else
|
||||||
serverError "Server configured without C run-time support" ""
|
serverError "Server configured without C run-time support" ""
|
||||||
#endif
|
#endif
|
||||||
_ -> pgfMain command =<< liftIO (readCache' (fst cache) path)
|
_ -> pgfMain command =<< getFile (readCache' (fst cache)) path
|
||||||
|
|
||||||
|
getFile get path =
|
||||||
|
either failed return =<< liftIO (E.try (get path))
|
||||||
|
where
|
||||||
|
failed e = if isDoesNotExistError e
|
||||||
|
then notFound path
|
||||||
|
else liftIO $ ioError e
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
-- * C run-time functionality
|
-- * C run-time functionality
|
||||||
@@ -435,6 +443,7 @@ toBool s = s `elem` ["","yes","true","True"]
|
|||||||
missing = badRequest "Missing parameter"
|
missing = badRequest "Missing parameter"
|
||||||
errorMissingId = badRequest "Missing identifier" ""
|
errorMissingId = badRequest "Missing identifier" ""
|
||||||
|
|
||||||
|
notFound = throw 404 "Not found"
|
||||||
badRequest = throw 400
|
badRequest = throw 400
|
||||||
serverError = throw 500
|
serverError = throw 500
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user