Guilhem Soulas
Guilhem Soulas

Reputation: 1995

Elm: Chain Http.send and Http.get

I'm (a beginner) having a type issue trying to chain to HTTP calls in my Elm application:

Http.send ... `Task.andThen` (\_ -> Http.get ...)

This is because Http.send return type is Task RawError Response, and Http.get return type is Task Error value.

Any suggestion on how to make them work together?

EDIT1:

Maybe mapError is the solution?

EDIT2:

I'm not saying that the first call failed, I'm sure it works. It is the compiler that doesn't validate my code:

The right argument of `andThen` is causing a type mismatch.

135│     Http.send Http.defaultSettings config
136│>      `Task.andThen` (\_ -> Http.get (Json.Decode.list userJsonDecoder) "http://localhost:3000/")

`andThen` is expecting the right argument to be a:

    Http.Response -> Task Http.RawError a

But the right argument is:

    Http.Response -> Task Http.Error (List User)

Upvotes: 2

Views: 559

Answers (1)

robertjlooby
robertjlooby

Reputation: 7220

You need a way to map a RawError to an Error and then you can use Task.mapError as you suggested in your first edit. One possibility would be:

rawErrorToError : Http.RawError -> Http.Error
rawErrorToError rawError =
  case rawError of
    Http.RawTimeout -> Http.Timeout
    Http.RawNetworkError -> Http.NetworkError

Http.send Http.defaultSettings config
|> Task.mapError rawErrorToError
`Task.andThen` ...

Upvotes: 1

Related Questions