Last active
May 7, 2019 13:38
-
-
Save jzxhuang/7f39149a671d0a8e285c48014e662845 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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