I'm wondering how to decode an Http.Error in elm and store it's data on my model.
I know that the error response is going to return this structure.
{
error: "Some error message",
ok: false
}
Here are my models and my Http request
type alias Model =
{ url = String
, result : String
, errorMessage : String
, error : Bool
}
model : Model
model =
{ url = ""
, result = ""
, errorMessage = ""
, error = False
}
-- make the request
makeRequest : String -> Cmd Msg
makeRequest url =
Task.perform FetchFail FetchSucceed (Http.get decodeTitle url)
-- decode the success response
decodeTitle : Json.Decoder String
decodeTitle =
Json.at ["title"] Json.string
-- decode the error
decodeError =
Json.object2 User
("error" := Json.string)
("ok" := Json.bool)
I was hoping I could handle this in FetchFail of my update like so.
type Msg
= FetchTitle
| FetchSucceed String
| FetchFail Http.Error
update : Msg -> Model -> (Model, Cmd Msg)
update action model =
case action of
...
FetchFail err ->
let
error =
decodeError error
in
({ model | ok = error.ok, errorMessage = error.error}, Cmd.none)
Any help is appreciated.
Errortype alias seems unused. Better remove it (from the question if it was used elsewhere). - farmio