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