Snap中未捕获的运行时异常的默认行为是显示包含错误的白页。这对于开发模式非常有用,但对于生产来说确实很糟糕。
我的应用程序设置可以捕获异常并记录错误,以便在发生不良情况时显示出对用户更具吸引力的内容。如何更改它,使其仅在生产模式下才会发生?
app :: SnapletInit App App
app = makeSnaplet "app" "An snaplet example application." Nothing $ do
h <- nestSnaplet "" heist $ heistInit "templates"
s <- nestSnaplet "sess" sess $
initCookieSessionManager "site_key.txt" "sess" (Just 86400)
d <- nestSnaplet "db" db pgsInit
addRoutes routes
wrapSite (<|> notFound)
-- TODO: figure out how make this only catch errors when in production mode
wrapSite internalServerError
initFlashNotice h sess
addConfig h $ mempty & scInterpretedSplices .~ userSessionSplices sess
return $ App h s d
internalServerError :: AppHandler () -> AppHandler ()
internalServerError h =
catch h (\ (e :: SomeException) -> do
logError $ B.pack $ show e
modifyResponse (setResponseCode 500)
render "errors/500")
Snap为此提供了getEnvironment函数。此函数获取传递给runSnaplet的环境。环境默认为“ devel”,但可以在运行时使用-e命令行选项进行更改。如果您像这样运行您的应用程序:
dist/build/myapp/myapp -e prod
然后getEnvironment
将返回“ prod”。您可以这样使用:
notFound = do
e <- getEnvironment
case e of
"devel" -> develErrorPage
_ -> productionErrorPage
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句