I'm attempting to complete an exercise in the Elm 0.17 tutorial for HTTP. If fetching a gif fails, I would like to let the user know why it failed with an error message.
I've modified my model to be:
type alias Model =
{ topic : String
, gifUrl : String
, errorMessage : String
}
And fetch fail in update to be:
FetchFail error ->
(Model model.topic model.gifUrl (errorMessage error), Cmd.none)
Where the function errorMessage
is as follows:
errorMessage : Http.Error -> String
errorMessage error =
case error of
Http.Timeout ->
"Timeout"
Http.NetworkError ->
"Network Error"
Http.UnexpectedPayload _ ->
"UnexpectedPayload"
Http.BadResponse _ _ ->
"BadResponse"
The above function seems like unnecessary boiler plate to me. Is there a way that I can directly convert Http.Error
into a string?