[elm] add function to allow more fine grained error handling#13773
[elm] add function to allow more fine grained error handling#13773eriktim merged 1 commit intoOpenAPITools:masterfrom
Conversation
to allow more fine grained error handling.
|
|
||
|
|
||
| sendWithCustomExpect : (Json.Decode.Decoder a -> Http.Expect msg) -> Request a -> Cmd msg | ||
| sendWithCustomExpect expect (Request req) = |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 msgThere was a problem hiding this comment.
Ah yes, that makes sense to support. Thanks for clarifying!
|
@wing328 is the failed build to be expected? |
|
@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 |
|
Thanks, @7omb! |
|
Thank you for talking the time to review the PR @eriktim |
This is a backwards compatible change. It adds the additional function
sendWithCustomExpectto the provided helper functions inApi.elmto 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
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.
master(6.1.0) (minor release - breaking changes with fallbacks),7.0.x(breaking changes without fallbacks)