Distinguishing HTTP error codes

92 views
Skip to first unread message

Michal Blazejczyk

unread,
Dec 3, 2017, 7:41:09 AM12/3/17
to Elm Discuss
Hi all,

I'm just beginning to get into Elm, and I encountered an issue.  My Elm app is making a POST request (Http.post) and when the server returns 400 or 401, I get NetworkError instead of BadStatus which means that I cannot distinguish between error codes.

Looking at Javascript generated by the Elm compiler, I see:

function toTask(request, maybeProgress)
{
 
// ...
  xhr
.addEventListener('error', function() {
    callback
(_elm_lang$core$Native_Scheduler.fail({ ctor: 'NetworkError' }));
 
});
  xhr
.addEventListener('timeout', function() {
    callback
(_elm_lang$core$Native_Scheduler.fail({ ctor: 'Timeout' }));
 
});
  xhr
.addEventListener('load', function() {
    callback
(handleResponse(xhr, request.expect.responseToResult));
 
});
 
// ...
}


For 400 and 401 responses, the browser triggers the listener for 'error'.  Is there a way to have Elm generate a BadStatus in this case?

Best,
Michal

Rafał Cieślak

unread,
Dec 3, 2017, 8:04:17 AM12/3/17
to Elm Discuss
Did you look into Http.expectStringResponse? I've never used it, but the docs say:

Maybe you want the whole Response: status code, headers, body, etc. This lets you get all of that information. From there you can use functions like Json.Decode.decodeString to interpret it as JSON or whatever else you want.

Peter Damoc

unread,
Dec 3, 2017, 8:59:01 AM12/3/17
to Elm Discuss
Are you able to confirm in the developer console or Postman or some other tool that you are actually getting a 401 from the server? 

I'm getting 400 and 401 just fine from my servers and Elm is encoding them just fine as a BadStatus. 



--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Michal Blazejczyk

unread,
Dec 3, 2017, 10:07:15 PM12/3/17
to Elm Discuss
Actually, this was a problem with CORS configuration on the server.  Once I fixed it, the problem in Elm went away.

Apologies and thank you  :)

Best,
Michal
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages