Last active
August 30, 2016 16:38
-
-
Save gotcha/9841a3db4946764c84466be313a4011f to your computer and use it in GitHub Desktop.
Elm accept empty HTTP response with 20x status
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
postTitle : String -> Model -> Task.Task Http.RawError Http.Response | |
postTitle url model = | |
let | |
token = | |
case model.token of | |
Just token -> | |
token | |
Nothing -> | |
"" | |
in | |
Http.send Http.defaultSettings | |
{ verb = "PATCH" | |
, headers = | |
[ ( "Accept", "application/json" ) | |
, ( "Content-Type", "application/json" ) | |
, ( "Authorization", "Bearer " ++ token ) | |
] | |
, url = url | |
, body = Http.string (titleJSON model) | |
} | |
updateTitle : Model -> Cmd Msg | |
updateTitle model = | |
let | |
url = | |
"http://localhost:8080/Plone/front-page" | |
in | |
Task.perform UpdateFail | |
UpdateSucceed | |
(isEmptyResponse (postTitle url model)) | |
isEmptyResponse : Task.Task Http.RawError Http.Response -> Task.Task Http.Error String | |
isEmptyResponse response = | |
let | |
isEmpty str = | |
if str == "" then | |
Task.succeed "ok" | |
else | |
Task.fail (Http.UnexpectedPayload "body is not empty") | |
in | |
Task.mapError promoteError response | |
`Task.andThen` handleResponse isEmpty | |
handleResponse : (String -> Task.Task Http.Error a) -> Http.Response -> Task.Task Http.Error a | |
handleResponse handle response = | |
if 200 <= response.status && response.status < 300 then | |
case response.value of | |
Http.Text str -> | |
handle str | |
_ -> | |
Task.fail (Http.UnexpectedPayload "Response body is a blob, expecting a string.") | |
else | |
Task.fail (Http.BadResponse response.status response.statusText) | |
promoteError : Http.RawError -> Http.Error | |
promoteError rawError = | |
case rawError of | |
Http.RawTimeout -> | |
Http.Timeout | |
Http.RawNetworkError -> | |
Http.NetworkError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment