Created
February 1, 2016 02:35
-
-
Save jasondew/6d6dd2e3356c17ba2cbe to your computer and use it in GitHub Desktop.
HTTP error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
update : Action -> Model -> ( Model, Effects Action ) | |
update action model = | |
case action of | |
HandleResponse result -> | |
case result of | |
Ok movies -> | |
( { model | movies = movies }, Effects.none ) | |
Err error -> | |
let | |
_ = reportError error | |
in | |
( model, Effects.none ) | |
search : String -> Effects Action | |
search query = | |
let | |
url = "/movies?query=" ++ query | |
in | |
Http.get moviesDecoder url | |
|> Task.toResult | |
|> Task.map HandleResponse | |
|> Effects.task | |
reportError : Http.Error -> Http.Error | |
reportError error = | |
case error of | |
Http.Timeout -> | |
Debug.log "API timeout" error | |
Http.NetworkError -> | |
Debug.log "Network error contacting API" error | |
Http.UnexpectedPayload payload -> | |
Debug.log ("Unexpected payload from API: " ++ payload) error | |
Http.BadResponse status payload -> | |
Debug.log ("Unexpected status/payload from API: " ++ (toString status) ++ "/" ++ payload) error | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment