Http Error BadPayload Response String or Response body?

Hello,
A simple question about the elm-lang/http 1.0.0 module.

How could Response String use in the BadPayload type definition while it refers to Response body i.e. a record ? What is the purpose or the goal of that?

type Error
     = BadPayload String (Response String)

type alias Response body = { url : String , 
    status : { code : Int, message : String } ,      
    headers : Dict String String , 
    body : body }

It’s been well explained in this good article that Response String is actually a record! So why not use Reponse body directly?! There should be a good reason!

 BadPayload message response ->
            "Bad Http Payload: "
                ++ toString message
                ++ " ("
                ++ toString response.status.code
                ++ ")"

Thanks for reading!

In that definition type alias Response body = ..., body is itself a type variable, same as if you said type alias Response a = ..., so when it defines BadPayload with Response String, it is just saying that the body will be a String rather than some other type.

1 Like

Tks for the type variable reminder, it was indeed not clear in my mind. But … I am still confused.

The body is a String where I can use record access field syntax on:

LoadArticle (Err (Http.BadPayload message response)) ->
  ...
  toString response.status.code

Is it not weird?


Edit:

Or should I consider Response String as a constructor having a string (the server response) as its argument and returning the record?
That sounds good to me.

Response is a type alias for a record representing the response, with the types of the body field of the response parametrized. In other words, Response theTypeOfBody is a convenient name for the shape of any record with this shape:

{ url : String
, status : { code : Int, message : String }
, headers : Dict String String
, body : theTypeOfBody
}

So Response String means you’re dealing with a record that has exactly the above shape, with the type of the body field set to String:

{ url : String
, status : { code : Int, message : String }
, headers : Dict String String
, body : String
}

When you’re dealing with a BadPayload error, this means that the body does not match what was expected. Most often, this means that a JSON decoder failed to decode it. In that case, having the Response record with the body as a plain String means you can log the exact string-value that your decoder failed to decode.

1 Like

Thanks, I got it!

type alias defined with type variablecan be implemented with concrete type parameter and then defines different types of record.

That’s great!