PaackEng / paack-remotedata / Remote.Response

This module exists for harmonizing queries results with the rest of this package.

Without it, the type of these results would need to be something like Response transportError (Response customError object).

Types


type Response transportError customError object
    = Failure (Remote.Errors.RemoteError transportError customError)
    | Success object

A Remote.Result is either Success meaning the query succeeded, or it is an Failure meaning that there was some failure.

A Failure is then sub-divided accordingly to Remote.Errors.


type alias GraphqlHttpResponse customError object =
Response (Graphql.Http.RawError () Graphql.Http.HttpError) customError object

While Response can model any type of errors, the most common one Paack has encountered is when fetching data from a Graphql query, and getting back a GraphqlError. Because of that, GraphqlHttpResponse is provided as a useful alias.

Creation

fromResults : Result transportError (Result customError object) -> Response transportError customError object

Tranforms Response transportError (Response customError object), received from a query result, into a Response transportError customError object.

NOTE: Prefer graphqlHttpToMsg and get Response directly in the resulting message.

graphqlHttpToMsg : (GraphqlHttpResponse customError object -> msg) -> Result (Graphql.Http.Error (Result customError object)) (Result customError object) -> msg

Prepares a message that will return GraphqlHttpResponse for Graphql usage.

query
    |> Graphql.Http.queryRequest "https://..."
    |> Graphql.Http.send
        (Response.graphqlHttpToMsg Msg.BookingFetched)

Identity crisis

isSuccess : Response transportError customError object -> Basics.Bool

True when Success _.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

isFailure : Response transportError customError object -> Basics.Bool

True when Failure _.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

isCustomError : Response transportError customError object -> Basics.Bool

True when Failure (Custom _).

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

isTransportError : Response transportError customError object -> Basics.Bool

True when Failure (Transport _).

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

Common transformations

toMaybe : Response transportError customError object -> Maybe object

Convert to a simpler Maybe if the actual error message is not needed or you need to interact with some code that primarily uses maybes.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

toError : Response transportError customError object -> Maybe (Remote.Errors.RemoteError transportError customError)

Transforms Failure error into Just error, and Success _ into Nothing.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

Generic transformations

map : (a -> b) -> Response transportError customError a -> Response transportError customError b

Apply a function to a successful value. If the result is Success, it will be converted. If the result is a Failure, the same error value will propagate through.

mapCustomError : (customError -> a) -> Response transportError customError object -> Response transportError a object

Transform a Failure (Custom a) value. If the result is Failure (Custom a), it will be converted. If the result is a Success _ or Failure (Transport _), the same value will propagate through.

mapTransportError : (transportError -> a) -> Response transportError customError object -> Response a customError object

Transform a Failure (Transport a) value. If the result is Failure (Transport a), it will be converted. If the result is a Success _ or Failure (Custom _), the same value will propagate through.

mapErrors : (Remote.Errors.RemoteError transportError customError -> a) -> Response transportError customError b -> Response a a b

Transform a Failure value. If the result is Failure, it will be converted. If the result is a Success, the same value will propagate through.

withDefault : object -> Response transportError customError object -> object

If the result is Success return the value, but if the result is a Failure then return a given default value.

NOTE: This function opposes the purpose of this package by eliminating not aimed states. Always evaluate using a switch-case instead.

merge : Response object object object -> object

For doing pipes instead of switch-case.

someResponse
    |> Response.map (always "Success")
    |> Response.mapCustomError (always "Expected failure")
    |> Response.mapTransportError (always "Network error")
    |> Response.merge