Is it possible Elm reports a BadStatus error as Network error for HTTP requests taking a long to complete?

I am having this code to handle errors I receive from my backend.

type alias ErrorData =
    { message : String
    , statusCode : Maybe Int
    }

type ApiRequestError
    = BadUrl ErrorData
    | Timeout ErrorData
    | NetworkError ErrorData
    | BadStatusWithErrorMessageParsed ErrorData
    | BadStatusWithErrorMessageNotParsed ErrorData
    | GoodStatusWithResponseParsingError ErrorData


expectJsonWithError : (Result ApiRequestError a -> msg) -> Decoder a -> Http.Expect msg
expectJsonWithError toMsg decoder =
    Http.expectStringResponse toMsg <|
        \response ->
            case response of
                Http.BadUrl_ url ->
                    Err (BadUrl <| ErrorData ("The requested url i.e. " ++ url ++ " is not valid") Nothing)

                Http.Timeout_ ->
                    Err <| Timeout <| ErrorData "The request has timed out" Nothing

                Http.NetworkError_ ->
                    Err <| NetworkError <| ErrorData "Looks like there is a problem with your network" Nothing

                Http.BadStatus_ { statusCode } body ->
                    case Decode.decodeString (Decode.at [ "error", "message" ] Decode.string) body of
                        Ok error ->
                            Err <| BadStatusWithErrorMessageParsed (ErrorData error (Just statusCode))

                        Err _ ->
                            Err <| BadStatusWithErrorMessageNotParsed (ErrorData "We couldn't parse the server response for an error message" (Just statusCode))

                Http.GoodStatus_ { statusCode } body ->
                    case Decode.decodeString decoder body of
                        Ok value ->
                            Ok value

                        Err err ->
                            Err <| GoodStatusWithResponseParsingError (ErrorData ("Response parsing error: " ++ Decode.errorToString err) (Just statusCode))

The problem I am having is that when I receive a HTTP response with a bad status code, elm is reporting it to be a network error. Please note that my HTTP request takes a very long to complete (almost 90 to 120 seconds). How can I make sure that Elm reports error correctly? Thank you

NetworkError is created from the error event of XMLHttpRequest: http/Http.js at 81b6fdc67d8e5fb25644fd79e6b0edbe2e14e474 · elm/http · GitHub

MDN has very little information on what the error event actually means: XMLHttpRequest: error event - Web APIs | MDN

This StackOverflow answer says the error event is only for network level errors, while a successful load event should always be triggered for any status code: javascript - What HTTP status codes fire error event on XMLHttpRequest - Stack Overflow

In short – Elm does not seem to be doing anything special here. It’s better to hunt for information on weirdness with XMLHttpRequest.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.