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

Conversation

7omb
Copy link
Contributor

@7omb 7omb commented Oct 20, 2022

This is a backwards compatible change. It adds the additional function sendWithCustomExpect to the provided helper functions in Api.elm to make requests.

It can be used to implement a more fine grained error handling as described here. Before it was e.g. not possible to access the response body if the server responded with an error like 404.

@eriktim

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (6.1.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

to allow more fine grained error handling.
Copy link
Contributor

@eriktim eriktim left a comment

Choose a reason for hiding this comment

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

Thanks for your PR @7omb. Please see the note.



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!

@eriktim
Copy link
Contributor

eriktim commented Oct 25, 2022

@wing328 is the failed build to be expected?

@wing328
Copy link
Member

wing328 commented Oct 26, 2022

@eriktim thanks for reviewing the change. The circleci build failure can be ignore.

when you've time, can you please PM me via Slack? thanks

https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g

@eriktim eriktim merged commit 009bf4c into OpenAPITools:master Oct 29, 2022
@eriktim
Copy link
Contributor

eriktim commented Oct 29, 2022

Thanks, @7omb!

@7omb 7omb deleted the generalize-elm-error-handling branch October 29, 2022 14:09
@7omb
Copy link
Contributor Author

7omb commented Oct 29, 2022

Thank you for talking the time to review the PR @eriktim

@wing328 wing328 added this to the 6.2.1 milestone Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants