Skip to content

Commit

Permalink
renderNotFound uses the same approach as normal 404 errors in IHP now.
Browse files Browse the repository at this point in the history
…Fixes #1180

renderNotFound now renders the normal not found page. It also respects the static/404.html if such a file exists.
  • Loading branch information
mpscholten committed Oct 28, 2021
1 parent bebf4e7 commit 4468735
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
15 changes: 13 additions & 2 deletions IHP/Controller/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import IHP.Controller.Layout
import qualified IHP.FrameworkConfig as FrameworkConfig
import qualified Data.ByteString.Builder as ByteString
import IHP.FlashMessages.ControllerFunctions (initFlashMessages)
import qualified IHP.ErrorController as ErrorController

renderPlain :: (?context :: ControllerContext) => LByteString -> IO ()
renderPlain text = respondAndExit $ responseLBS status200 [(hContentType, "text/plain")] text
Expand Down Expand Up @@ -89,9 +90,19 @@ renderJson' :: (?context :: ControllerContext) => ResponseHeaders -> Data.Aeson.
renderJson' additionalHeaders json = respondAndExit $ responseLBS status200 ([(hContentType, "application/json")] <> additionalHeaders) (Data.Aeson.encode json)
{-# INLINABLE renderJson' #-}

-- | Render's a generic not found page
--
-- This can be useful e.g. when an entity cannot be found:
--
-- > action ExampleAction = do
-- > renderNotFound
--
-- You can override the default not found error page by creating a new file at @static/404.html@. Then IHP will render that HTML file instead of displaying the default IHP not found page.
--
renderNotFound :: (?context :: ControllerContext) => IO ()
renderNotFound = renderPlain "Not Found"
{-# INLINABLE renderNotFound #-}
renderNotFound = do
response <- ErrorController.buildNotFoundResponse
respondAndExit response

data PolymorphicRender
= PolymorphicRender
Expand Down
16 changes: 10 additions & 6 deletions IHP/ErrorController.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module IHP.ErrorController
, handleNoResponseReturned
, handleNotFound
, handleRouterException
, buildNotFoundResponse
) where

import IHP.Prelude hiding (displayException)
Expand Down Expand Up @@ -55,16 +56,19 @@ handleNoResponseReturned controller = do
-- | Renders a 404 not found response. If a static/404.html exists, that is rendered instead of the IHP not found page
handleNotFound :: (?context :: RequestContext) => IO ResponseReceived
handleNotFound = do
hasCustomNotFound <- Directory.doesFileExist "static/404.html"
response <- if hasCustomNotFound
then customNotFoundResponse
else pure defaultNotFoundResponse

response <- buildNotFoundResponse
let RequestContext { respond } = ?context
respond response

buildNotFoundResponse :: forall context. (?context :: context, ConfigProvider context) => IO Response
buildNotFoundResponse = do
hasCustomNotFound <- Directory.doesFileExist "static/404.html"
if hasCustomNotFound
then customNotFoundResponse
else pure defaultNotFoundResponse

-- | The default IHP 404 not found page
defaultNotFoundResponse :: (?context :: RequestContext) => Response
defaultNotFoundResponse :: forall context. (?context :: context, ConfigProvider context) => Response
defaultNotFoundResponse = do
let errorMessage = [hsx|Router failed to find an action to handle this request.|]
let title = H.text "Action Not Found"
Expand Down

0 comments on commit 4468735

Please sign in to comment.