Discussion:
Distinguishing HTTP error codes
(too old to reply)
Michal Blazejczyk
2017-12-03 03:36:28 UTC
Permalink
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
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Rafał Cieślak
2017-12-03 13:04:16 UTC
Permalink
Did you look into Http.expectStringResponse
<http://package.elm-lang.org/packages/elm-lang/http/1.0.0/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.
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Peter Damoc
2017-12-03 13:58:52 UTC
Permalink
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.



On Sun, Dec 3, 2017 at 5:36 AM, Michal Blazejczyk <
Post by Michal Blazejczyk
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.
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
--
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
For more options, visit https://groups.google.com/d/optout.
--
There is NO FATE, we are the creators.
blog: http://damoc.ro/
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Michal Blazejczyk
2017-12-04 03:02:37 UTC
Permalink
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
Post by Peter Damoc
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.
Post by Michal Blazejczyk
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.
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
--
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
For more options, visit https://groups.google.com/d/optout.
--
There is NO FATE, we are the creators.
blog: http://damoc.ro/
--
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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...