mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-23 22:26:16 +03:00
Add error handling example to pokedex.
This commit is contained in:
parent
188d9bac5a
commit
49e5e26fd1
@ -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 =
|
||||
|
77
examples/pokedex/app/Route/ErrorHandling.elm
Normal file
77
examples/pokedex/app/Route/ErrorHandling.elm
Normal 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"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user