From 49e5e26fd1aecf26c04006464e2252ff459385dd Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Wed, 15 Mar 2023 11:07:13 -0700 Subject: [PATCH] Add error handling example to pokedex. --- examples/pokedex/app/ErrorPage.elm | 12 ++- examples/pokedex/app/Route/ErrorHandling.elm | 77 ++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 examples/pokedex/app/Route/ErrorHandling.elm diff --git a/examples/pokedex/app/ErrorPage.elm b/examples/pokedex/app/ErrorPage.elm index 339f503f..6e507ff2 100644 --- a/examples/pokedex/app/ErrorPage.elm +++ b/examples/pokedex/app/ErrorPage.elm @@ -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 = diff --git a/examples/pokedex/app/Route/ErrorHandling.elm b/examples/pokedex/app/Route/ErrorHandling.elm new file mode 100644 index 00000000..66e05572 --- /dev/null +++ b/examples/pokedex/app/Route/ErrorHandling.elm @@ -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" + ] + }