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

**URL:** <https://discourse.elm-lang.org/t/is-it-possible-elm-reports-a-badstatus-error-as-network-error-for-http-requests-taking-a-long-to-complete/8696>\
**Category:** Learn\
**Created:** [October 10, 2022, 2:10am UTC](https://discourse.elm-lang.org/t/is-it-possible-elm-reports-a-badstatus-error-as-network-error-for-http-requests-taking-a-long-to-complete/8696 "2022-10-10T02:10:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![segfaulter](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/segfaulter/32/2744_2.png) [@segfaulter](https://discourse.elm-lang.org/u/segfaulter)\
**Post date:** [October 10, 2022, 2:10am UTC](https://discourse.elm-lang.org/t/is-it-possible-elm-reports-a-badstatus-error-as-network-error-for-http-requests-taking-a-long-to-complete/8696/1 "2022-10-10T02:10:34Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [October 10, 2022, 8:14am UTC](https://discourse.elm-lang.org/t/is-it-possible-elm-reports-a-badstatus-error-as-network-error-for-http-requests-taking-a-long-to-complete/8696/2 "2022-10-10T08:14:03Z")

</div>

`NetworkError` is created from the `error` event of `XMLHttpRequest`: [http/Http.js at 81b6fdc67d8e5fb25644fd79e6b0edbe2e14e474 · elm/http · GitHub](https://github.com/elm/http/blob/81b6fdc67d8e5fb25644fd79e6b0edbe2e14e474/src/Elm/Kernel/Http.js#L25)

MDN has very little information on what the `error` event actually means: [XMLHttpRequest: error event - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/error_event)

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](https://stackoverflow.com/questions/14492534/what-http-status-codes-fire-error-event-on-xmlhttprequest)

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 20, 2022, 8:14am UTC](https://discourse.elm-lang.org/t/is-it-possible-elm-reports-a-badstatus-error-as-network-error-for-http-requests-taking-a-long-to-complete/8696/3 "2022-10-20T08:14:58Z")

</div>

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