defaultMessageWidget

This commit is contained in:
Michael Snoyman 2016-12-07 20:08:47 -05:00
parent 706a995b67
commit 9a484f9163
3 changed files with 34 additions and 34 deletions

View File

@ -1,3 +1,7 @@
## 1.4.30
* Add `defaultMessageWidget`
## 1.4.29 ## 1.4.29
* Exports some internals and fix version bounds [#1318](https://github.com/yesodweb/yesod/pull/1318) * Exports some internals and fix version bounds [#1318](https://github.com/yesodweb/yesod/pull/1318)

View File

@ -319,6 +319,19 @@ class RenderRoute site => Yesod site where
yesodWithInternalState :: site -> Maybe (Route site) -> (InternalState -> IO a) -> IO a yesodWithInternalState :: site -> Maybe (Route site) -> (InternalState -> IO a) -> IO a
yesodWithInternalState _ _ = bracket createInternalState closeInternalState yesodWithInternalState _ _ = bracket createInternalState closeInternalState
{-# INLINE yesodWithInternalState #-} {-# INLINE yesodWithInternalState #-}
-- | Convert a title and HTML snippet into a 'Widget'. Used
-- primarily for wrapping up error messages for better display.
--
-- @since 1.4.30
defaultMessageWidget :: Html -> HtmlUrl (Route site) -> WidgetT site IO ()
defaultMessageWidget title body = do
setTitle title
toWidget
[hamlet|
<h1>#{title}
^{body}
|]
{-# DEPRECATED urlRenderOverride "Use urlParamRenderOverride instead" #-} {-# DEPRECATED urlRenderOverride "Use urlParamRenderOverride instead" #-}
-- | Default implementation of 'makeLogger'. Sends to stdout and -- | Default implementation of 'makeLogger'. Sends to stdout and
@ -636,11 +649,7 @@ defaultErrorHandler NotFound = selectRep $ do
provideRep $ defaultLayout $ do provideRep $ defaultLayout $ do
r <- waiRequest r <- waiRequest
let path' = TE.decodeUtf8With TEE.lenientDecode $ W.rawPathInfo r let path' = TE.decodeUtf8With TEE.lenientDecode $ W.rawPathInfo r
setTitle "Not Found" defaultMessageWidget "Not Found" [hamlet|<p>#{path'}|]
toWidget [hamlet|
<h1>Not Found
<p>#{path'}
|]
provideRep $ return $ object ["message" .= ("Not Found" :: Text)] provideRep $ return $ object ["message" .= ("Not Found" :: Text)]
-- For API requests. -- For API requests.
@ -648,12 +657,9 @@ defaultErrorHandler NotFound = selectRep $ do
-- if you specify an authRoute the user will be redirected there and -- if you specify an authRoute the user will be redirected there and
-- this page will not be shown. -- this page will not be shown.
defaultErrorHandler NotAuthenticated = selectRep $ do defaultErrorHandler NotAuthenticated = selectRep $ do
provideRep $ defaultLayout $ do provideRep $ defaultLayout $ defaultMessageWidget
setTitle "Not logged in" "Not logged in"
toWidget [hamlet| [hamlet|<p style="display:none;">Set the authRoute and the user will be redirected there.|]
<h1>Not logged in
<p style="display:none;">Set the authRoute and the user will be redirected there.
|]
provideRep $ do provideRep $ do
-- 401 *MUST* include a WWW-Authenticate header -- 401 *MUST* include a WWW-Authenticate header
@ -670,20 +676,16 @@ defaultErrorHandler NotAuthenticated = selectRep $ do
return $ object $ ("message" .= ("Not logged in"::Text)):content return $ object $ ("message" .= ("Not logged in"::Text)):content
defaultErrorHandler (PermissionDenied msg) = selectRep $ do defaultErrorHandler (PermissionDenied msg) = selectRep $ do
provideRep $ defaultLayout $ do provideRep $ defaultLayout $ defaultMessageWidget
setTitle "Permission Denied" "Permission Denied"
toWidget [hamlet| [hamlet|<p>#{msg}|]
<h1>Permission denied
<p>#{msg}
|]
provideRep $ provideRep $
return $ object ["message" .= ("Permission Denied. " <> msg)] return $ object ["message" .= ("Permission Denied. " <> msg)]
defaultErrorHandler (InvalidArgs ia) = selectRep $ do defaultErrorHandler (InvalidArgs ia) = selectRep $ do
provideRep $ defaultLayout $ do provideRep $ defaultLayout $ defaultMessageWidget
setTitle "Invalid Arguments" "Invalid Arguments"
toWidget [hamlet| [hamlet|
<h1>Invalid Arguments
<ul> <ul>
$forall msg <- ia $forall msg <- ia
<li>#{msg} <li>#{msg}
@ -692,20 +694,14 @@ defaultErrorHandler (InvalidArgs ia) = selectRep $ do
defaultErrorHandler (InternalError e) = do defaultErrorHandler (InternalError e) = do
$logErrorS "yesod-core" e $logErrorS "yesod-core" e
selectRep $ do selectRep $ do
provideRep $ defaultLayout $ do provideRep $ defaultLayout $ defaultMessageWidget
setTitle "Internal Server Error" "Internal Server Error"
toWidget [hamlet| [hamlet|<pre>#{e}|]
<h1>Internal Server Error
<pre>#{e}
|]
provideRep $ return $ object ["message" .= ("Internal Server Error" :: Text), "error" .= e] provideRep $ return $ object ["message" .= ("Internal Server Error" :: Text), "error" .= e]
defaultErrorHandler (BadMethod m) = selectRep $ do defaultErrorHandler (BadMethod m) = selectRep $ do
provideRep $ defaultLayout $ do provideRep $ defaultLayout $ defaultMessageWidget
setTitle"Bad Method" "Method Not Supported"
toWidget [hamlet| [hamlet|<p>Method <code>#{S8.unpack m}</code> not supported|]
<h1>Method Not Supported
<p>Method <code>#{S8.unpack m}</code> not supported
|]
provideRep $ return $ object ["message" .= ("Bad method" :: Text), "method" .= TE.decodeUtf8With TEE.lenientDecode m] provideRep $ return $ object ["message" .= ("Bad method" :: Text), "method" .= TE.decodeUtf8With TEE.lenientDecode m]
asyncHelper :: (url -> [x] -> Text) asyncHelper :: (url -> [x] -> Text)

View File

@ -1,5 +1,5 @@
name: yesod-core name: yesod-core
version: 1.4.29 version: 1.4.30
license: MIT license: MIT
license-file: LICENSE license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com> author: Michael Snoyman <michael@snoyman.com>