Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[elm] add function to allow more fine grained error handling #13773

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions modules/openapi-generator/src/main/resources/elm/Api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Api exposing
, request
, send
, sendWithCustomError
, sendWithCustomExpect
, task
, map
, withBasePath
Expand Down Expand Up @@ -55,13 +56,18 @@ send toMsg req =


sendWithCustomError : (Http.Error -> e) -> (Result e a -> msg) -> Request a -> Cmd msg
sendWithCustomError mapError toMsg (Request req) =
sendWithCustomError mapError toMsg req =
sendWithCustomExpect (expectJson mapError toMsg) req


sendWithCustomExpect : (Json.Decode.Decoder a -> Http.Expect msg) -> Request a -> Cmd msg
sendWithCustomExpect expect (Request req) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I'm following why this takes a decoder to message function. Can you give me a basic example of what you would to with it?

Copy link
Contributor Author

@7omb 7omb Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The decoders in the signature are the ones provided by the Requests from Api/Request/Default.elm. To use sendWithCustomExpect one has to provide the logic to interpret the response body given the decoders.

My goal was make it possible to use a custom error type (the current send and sendWithCustomError both expect a Http.Error). A shortened example for String that e.g. accesses the body of a Http.BadStatus_ could e.g. look like this:

customSend : (Result String a -> msg) -> Api.Request a -> Cmd msg
customSend toMsg req =
    Api.sendWithCustomExpect (Http.expectStringResponse toMsg << decodeResponse) req


decodeResponse : Json.Decode.Decoder a -> Http.Response String -> Result String a
decodeResponse decoder response =
    case response of
        ...
        Http.BadStatus_ _ body ->
            Err body

        ...

Where decodeResponse goes from:

Json.Decode.Decoder a -> (Http.Response String -> Result String a)

and expectStringResponse toMsg from:

(Response String -> Result String a) -> Expect msg

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that makes sense to support. Thanks for clarifying!

Http.request
{ method = req.method
, headers = req.headers
, url = Url.Builder.crossOrigin req.basePath req.pathParams req.queryParams
, body = req.body
, expect = expectJson mapError toMsg req.decoder
, expect = expect req.decoder
, timeout = req.timeout
, tracker = req.tracker
}
Expand Down
10 changes: 8 additions & 2 deletions samples/openapi3/client/elm/src/Api.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Api exposing
, request
, send
, sendWithCustomError
, sendWithCustomExpect
, task
, map
, withBasePath
Expand Down Expand Up @@ -55,13 +56,18 @@ send toMsg req =


sendWithCustomError : (Http.Error -> e) -> (Result e a -> msg) -> Request a -> Cmd msg
sendWithCustomError mapError toMsg (Request req) =
sendWithCustomError mapError toMsg req =
sendWithCustomExpect (expectJson mapError toMsg) req


sendWithCustomExpect : (Json.Decode.Decoder a -> Http.Expect msg) -> Request a -> Cmd msg
sendWithCustomExpect expect (Request req) =
Http.request
{ method = req.method
, headers = req.headers
, url = Url.Builder.crossOrigin req.basePath req.pathParams req.queryParams
, body = req.body
, expect = expectJson mapError toMsg req.decoder
, expect = expect req.decoder
, timeout = req.timeout
, tracker = req.tracker
}
Expand Down