Skip to content

Commit

Permalink
Merge pull request #304 from digitallyinduced/error-controller-updates
Browse files Browse the repository at this point in the history
Error Controller Updates
  • Loading branch information
Laobiz authored Aug 7, 2020
2 parents d262988 + 21f2ebf commit c9cb949
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 58 deletions.
55 changes: 52 additions & 3 deletions IHP/ErrorController.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
module IHP.ErrorController (displayException, handleNoResponseReturned, handleNotFound) where
{-|
Module: IHP.ErrorController
Description: Provides web-based error screens for runtime errors in IHP
Copyright: (c) digitally induced GmbH, 2020
-}
module IHP.ErrorController
( displayException
, handleNoResponseReturned
, handleNotFound
, handleRouterException
) where

import IHP.Prelude hiding (displayException)
import qualified Control.Exception as Exception
Expand All @@ -19,10 +29,8 @@ import qualified Database.PostgreSQL.Simple.FromField as PG
import qualified Data.ByteString.Char8 as ByteString

import IHP.HtmlSupport.QQ (hsx)

import Database.PostgreSQL.Simple.FromField (ResultError (..))


handleNoResponseReturned :: (Show controller, ?requestContext :: RequestContext) => controller -> IO ResponseReceived
handleNoResponseReturned controller = do
let codeSample :: Text = "render MyView { .. }"
Expand All @@ -45,6 +53,19 @@ handleNotFound = do
let (RequestContext _ respond _ _ _) = ?requestContext
respond $ responseBuilder status404 [(hContentType, "text/html")] (Blaze.renderHtmlBuilder (renderError title errorMessage))

handleRouterException :: (?requestContext :: RequestContext) => SomeException -> IO ResponseReceived
handleRouterException exception = do
let errorMessage = [hsx|
Routing failed with: {tshow exception}

<h2>Possible Solutions</h2>
<p>Are you using AutoRoute but some of your fields are not UUID? In that case <a href="https://ihp.digitallyinduced.com/Guide/routing.html#parameter-types" target="_blank">please see the documentation on Parameter Types</a></p>
|]
let title = H.text "Routing failed"
let (RequestContext _ respond _ _ _) = ?requestContext
respond $ responseBuilder status500 [(hContentType, "text/html")] (Blaze.renderHtmlBuilder (renderError title errorMessage))


displayException :: (Show action, ?requestContext :: RequestContext) => SomeException -> action -> Text -> IO ResponseReceived
displayException exception action additionalInfo = do
let allHandlers = [ postgresHandler, patternMatchFailureHandler ]
Expand Down Expand Up @@ -139,6 +160,28 @@ renderError errorTitle view = H.docTypeHtml ! A.lang "en" $ [hsx|
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Helvetica Neue", Arial, sans-serif;
}

body a {
color: hsla(196, 13%, 80%, 1);
}

.ihp-error-other-solutions {
margin-top: 2rem;
padding-top: 0.5rem;
font-size: 1rem;
color: hsla(196, 13%, 80%, 1);
border-top: 1px solid hsla(196, 13%, 60%, 0.4);
}

.ihp-error-other-solutions a {
color: hsla(196, 13%, 80%, 0.9);
text-decoration: none !important;
margin-right: 1rem;
font-size: 0.8rem;
}
.ihp-error-other-solutions a:hover {
color: hsla(196, 13%, 80%, 1);
}
</style>
</head>
<body>
Expand All @@ -148,6 +191,12 @@ renderError errorTitle view = H.docTypeHtml ! A.lang "en" $ [hsx|
<div style="margin-top: 1rem; font-size: 1.25rem; color:hsla(196, 13%, 80%, 1)">
{view}
</div>

<div class="ihp-error-other-solutions">
<a href="https://gitter.im/digitallyinduced/ihp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge" target="_blank">Ask the IHP Community on Gitter</a>
<a href="https://github.com/digitallyinduced/ihp/wiki/Troubleshooting" target="_blank">Check the Troubleshooting</a>
<a href="https://github.com/digitallyinduced/ihp/issues/new" target="_blank">Open a GitHub Issue</a>
</div>
</div>
</div>
</body>
Expand Down
8 changes: 8 additions & 0 deletions IHP/HaskellSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module IHP.HaskellSupport (
, isToday
, isToday'
, forEach
, forEachWithIndex
, textToInt
, isWeekend
, todayIsWeekend
Expand Down Expand Up @@ -174,6 +175,13 @@ forEach :: (MonoFoldable mono, Applicative m) => mono -> (Element mono -> m ())
forEach elements function = forM_ elements function
{-# INLINE forEach #-}


-- | Example:
-- forEachWithIndex users \(index, user) -> putStrLn (tshow user)
forEachWithIndex :: (Applicative m) => [a] -> ((Int, a) -> m ()) -> m ()
forEachWithIndex elements function = forM_ (ClassyPrelude.zip [0..] elements) function
{-# INLINE forEachWithIndex #-}

-- | Parses a text to an int. Returns @Nothing@ on failure.
--
-- __Example:__
Expand Down
9 changes: 9 additions & 0 deletions IHP/IDE/Logs/Controller.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module IHP.IDE.Logs.Controller where

import IHP.ControllerPrelude
import IHP.IDE.ToolServer.Helper.Controller
import IHP.IDE.ToolServer.Types
import IHP.IDE.ToolServer.ViewContext
import IHP.IDE.Logs.View.Logs
Expand Down Expand Up @@ -37,6 +38,14 @@ instance Controller LogsController where

render LogsView { .. }

action OpenEditorAction = do
let path = param @Text "path"
let line = paramOrDefault @Int 0 "line"
let col = paramOrDefault @Int 0 "col"
openEditor path line col

renderPlain ""

readDevServerState :: (?controllerContext :: ControllerContext) => IO DevServer.AppState
readDevServerState = theDevServerContext
|> get #appStateRef
Expand Down
Loading

0 comments on commit c9cb949

Please sign in to comment.