concept error in category elm

appears as: errors, n error, error, errors, error
Elm in Action

This is an excerpt from Manning's book Elm in Action.

  • Elm’s compiler would catch a lot of the errors I’d been seeing before they could harm me in production. I did not yet know just how many it would catch.
  • Notice that the Result we get back from decodeString has one concrete type—namely, that errValue is always an Error—and one that matches the type of the given decoder’s type variable. (Error is a custom type that the Json.Decode module uses to represent an error that occurred during decoding.) Table 4.3 shows how the decoder passed to decodeString affects its return type.

    What’s BadBody? It’s a variant of that Http.Error type we said we’d get back to. (Sure enough, here we are getting back to it.) Http.Error is a custom type that describes various ways an HTTP request can fail. It looks like this:

    Listing 4.7. The complete PhotoGroove.elm
    module PhotoGroove exposing (main)
    
    import Browser
    import Html exposing (..)
    import Html.Attributes exposing (class, classList, id, name, src, title, type_)
    import Html.Events exposing (onClick)
    import Http
    import Json.Decode exposing (Decoder, bool, int, list, string, succeed)
    import Json.Decode.Pipeline exposing (optional, required)
    import Random
    
     
    urlPrefix : String
    urlPrefix =
        "http://elm-in-action.com/"
    
     
    type Msg
        = ClickedPhoto String
        | ClickedSize ThumbnailSize
        | ClickedSurpriseMe
        | GotRandomPhoto Photo
        | GotPhotos (Result Http.Error (List Photo))
    
     
    view : Model -> Html Msg
    view model =
        div [ class "content" ] <|                                #1
            case model.status of
                Loaded photos selectedUrl ->
                    viewLoaded photos selectedUrl model.chosenSize
    
                Loading ->
                    []
    
                Errored errorMessage ->
                    [ text ("Error: " ++ errorMessage) ]
    
     
    viewLoaded : List Photo -> String -> ThumbnailSize -> List (Html Msg)
    viewLoaded photos selectedUrl chosenSize =
        [ h1 [] [ text "Photo Groove" ]
        , button
            [ onClick ClickedSurpriseMe ]
            [ text "Surprise Me!" ]
        , h3 [] [ text "Thumbnail Size:" ]
        , div [ id "choose-size" ]
            (List.map viewSizeChooser [ Small, Medium, Large ])
        , div [ id "thumbnails", class (sizeToString chosenSize) ]
            (List.map (viewThumbnail selectedUrl) photos)
        , img
            [ class "large"
            , src (urlPrefix ++ "large/" ++ selectedUrl)
            ]
            []
        ]
    
     
    viewThumbnail : String -> Photo -> Html Msg
    viewThumbnail selectedUrl thumb =
        img
            [ src (urlPrefix ++ thumb.url)
            , title (thumb.title ++ " [" ++ String.fromInt thumb.size ++ " KB]")
            , classList [ ( "selected", selectedUrl == thumb.url ) ]
            , onClick (ClickedPhoto thumb.url)
            ]
            []
    
     
    viewSizeChooser : ThumbnailSize -> Html Msg
    viewSizeChooser size =
        label []
            [ input [ type_ "radio", name "size", onClick (ClickedSize size) ] []
            , text (sizeToString size)
            ]
    
     
    sizeToString : ThumbnailSize -> String
    sizeToString size =
        case size of
            Small ->
                "small"
    
            Medium ->
                "med"
    
            Large ->
                "large"
    
     
    type ThumbnailSize
        = Small
        | Medium
        | Large
    
     
    type alias Photo =
        { url : String
        , size : Int
        , title : String
        }
    
     
    
    photoDecoder : Decoder Photo
    photoDecoder =
        succeed Photo                                                 #2
            |> required "url" string                                  #3
            |> required "size" int                                    #4
            |> optional "title" string "(untitled)                    #5
    
     
    type Status
        = Loading
        | Loaded (List Photo) String
        | Errored String
    
     
    type alias Model =
        { status : Status
        , chosenSize : ThumbnailSize
        }
    
     
    initialModel : Model
    initialModel =
        { status = Loading
        , chosenSize = Medium
        }
    
     
    update : Msg -> Model -> ( Model, Cmd Msg )
    update msg model =
        case msg of
            GotRandomPhoto photo ->
                ( { model | status = selectUrl photo.url model.status }, Cmd.none )
    
            ClickedPhoto url ->
                ( { model | status = selectUrl url model.status }, Cmd.none )
    
            ClickedSize size ->
                ( { model | chosenSize = size }, Cmd.none )
    
            ClickedSurpriseMe ->
                case model.status of
                    Loaded (firstPhoto :: otherPhotos) _ ->          #6
                        Random.uniform firstPhoto otherPhotos        #7
                            |> Random.generate GotRandomPhoto        #7
                            |> Tuple.pair model                      #7
    
                    Loaded [] _ ->                                   #8
                        ( model, Cmd.none )
    
                    Loading ->
                        ( model, Cmd.none )
    
                    Errored errorMessage ->
                        ( model, Cmd.none )
    
            GotPhotos (Ok photos) ->                                  #9
                case photos of
                    first :: rest ->
                        ( { model | status = Loaded photos first.url }
                        , Cmd.none
                        )
    
                    [] ->
                        ( { model | status = Errored "0 photos found" }, Cmd.none )
    
            GotPhotos (Err httpError) ->
                ( { model | status = Errored "Server error!" }, Cmd.none )
    
     
    selectUrl : String -> Status -> Status
    selectUrl url status =
        case status of
            Loaded photos _ ->
                Loaded photos url
    
            Loading ->
                status
    
            Errored errorMessage ->
                status
    
     
    initialCmd : Cmd Msg
    initialCmd =
        Http.get
            { url = "http://elm-in-action.com/photos/list.json"
            , expect = Http.expectJson GotPhotos (list photoDecoder)  #10
            }
    
     
    main : Program () Model Msg
    main =
        Browser.element
            { init = \flags -> ( initialModel, initialCmd )
            , view = view
            , update = update
            , subscriptions = \_ -> Sub.none
            }