Skip to content

Instantly share code, notes, and snippets.

@jzxhuang
Last active May 7, 2019 13:38
Show Gist options
  • Save jzxhuang/7f39149a671d0a8e285c48014e662845 to your computer and use it in GitHub Desktop.
Save jzxhuang/7f39149a671d0a8e285c48014e662845 to your computer and use it in GitHub Desktop.
import Http
import Json.Decode as D
expectJson : (Result Http.Error a -> msg) -> D.Decoder a -> Expect msg
expectJson toMsg decoder =
expectStringResponse toMsg <|
\response ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)
Http.Timeout_ ->
Err Http.Timeout
Http.NetworkError_ ->
Err Http.NetworkError
Http.BadStatus_ metadata body ->
Err (Http.BadStatus metadata.statusCode)
Http.GoodStatus_ metadata body ->
case D.decodeString decoder body of
Ok value ->
Ok value
Err err ->
Err (Http.BadBody (D.errorToString err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment