Add error handling example to pokedex.

This commit is contained in:
Dillon Kearns 2023-03-15 11:07:13 -07:00
parent 188d9bac5a
commit 49e5e26fd1
2 changed files with 88 additions and 1 deletions

View File

@ -102,7 +102,7 @@ view error model =
, title = "Invalid pokedex number"
}
_ ->
NotFound ->
{ body =
[ Html.div []
[ Html.p [] [ Html.text "Page not found. Maybe try another URL?" ]
@ -121,6 +121,16 @@ view error model =
, title = "This is a NotFound Error"
}
InternalError errorMessage ->
{ body =
[ Html.div []
[ Html.h2 [] [ Html.text "Something's Not Right Here" ]
, Html.p [] [ Html.text errorMessage ]
]
]
, title = "Internal Server Error"
}
statusCode : ErrorPage -> number
statusCode error =

View File

@ -0,0 +1,77 @@
module Route.ErrorHandling exposing (ActionData, Data, Model, Msg, route)
import BackendTask exposing (BackendTask)
import ErrorPage exposing (ErrorPage)
import FatalError exposing (FatalError)
import Head
import Html exposing (text)
import PagesMsg exposing (PagesMsg)
import RouteBuilder exposing (App, StatefulRoute, StatelessRoute)
import Server.Request as Request exposing (Parser)
import Server.Response as Response exposing (Response)
import Shared
import View exposing (View)
type alias Model =
{}
type alias Msg =
()
type alias RouteParams =
{}
type alias ActionData =
{}
route : StatelessRoute RouteParams Data ActionData
route =
RouteBuilder.serverRender
{ head = head
, data = data
, action = \_ -> Request.skip "No action."
}
|> RouteBuilder.buildNoState { view = view }
type alias Data =
{ darkMode : Maybe String }
data : RouteParams -> Parser (BackendTask FatalError (Response Data ErrorPage))
data routeParams =
Request.succeed
(BackendTask.fail
(FatalError.fromString "This error should be displayed by the error handling!")
)
head :
App Data ActionData RouteParams
-> List Head.Tag
head static =
[]
view :
App Data ActionData RouteParams
-> Shared.Model
-> View (PagesMsg Msg)
view static sharedModel =
{ title = "Cookie test"
, body =
[ case static.data.darkMode of
Just darkMode ->
text <|
"Dark mode: "
++ darkMode
Nothing ->
text "No dark mode preference set"
]
}