Conversation
lib/req/unexpected_response_error.ex
Outdated
| @@ -0,0 +1,16 @@ | |||
| defmodule Req.UnexpectedResponseError do | |||
| defexception [:expected, :response] | |||
There was a problem hiding this comment.
maybe instead of storing the full response we keep just:
| defexception [:expected, :response] | |
| defexception [:expected_status, :status, :headers, :body] |
i.e. no resp.private (nor resp.assigns, #492)
|
Maybe we put it after decompress but before decode so the exception message just prints the string |
|
I still feel
At least for most OpenAPI, non-redirecting APIs; which are the most commons anyway. I think it is worth it to keep it around, and trivial enough where it doesn't add much complexity, if any. |
|
Thanks for the feedback about expect. I concede response.ok is ubiquitous due to being part of the Web platform. I think in pattern matches, by the way of falling into another code branch that is for transport errors, expect "wins": case resp do
- {:ok, %{ok?: true} = resp -> ...
- {:ok, resp} -> ...
- {:error, e} -> ...
+ {:ok, resp} ->
+ {:error, e} -> ...
endIn other cases - if resp.status in 200..299 do
+ if resp.ok? doyeah, that looks really clean. My personal opinion though is it doesn't justify adding a new field to the struct even if that's some false scarcity argument. |
|
Most likely useful for default steps behaviour and things like that, purely a semantic meaning that sometimes it is useful. I have come across that |
72e20cd to
f284e1a
Compare
| iex> resp.status | ||
| 200 | ||
|
|
||
| iex> Req.get!("https://httpbin.org/status/404", expect: 200..299) |
There was a problem hiding this comment.
| iex> Req.get!("https://httpbin.org/status/404", expect: 200..299) | |
| iex> Req.get!("https://httpbin.org/status/404", expect: :ok) |
could be an atom representing a given config as well? 😅
There was a problem hiding this comment.
Good question! Is :ok the only accepted atom?
If yes, I could maybe see that.
If not, I guess we support :not_found and friends? I think this reads well expect: [:ok, :not_found] and we could totally find typos (and Did you mean it) in atom names. But then again:
iex> Plug.Conn.Status.code(:ok)
200
iex> Plug.Conn.Status.code(:not_found)
404
so we probably shouldn't call it :ok because it's not clear whether it's the 200 OK or all 2XXs. The RFC groups statuses into:
- 1xx (Informational)
- 2xx (Successful)
- 3xx (Redirection)
- 4xx (Client Error)
- 5xx (Server Error)
but expect: :successful doesn't quite have the same ring to it.
So yeah, not sure. WDYT?
There was a problem hiding this comment.
expect: :success looks pretty good to me!
There was a problem hiding this comment.
I would use RFC naming, althou somewhat annoying, it is the RFC so we avoid ambiguous language.
|
Is there anything I can do to help move this forward? I've been waiting for this feature for a long time now 🫠 |
|
I'm merging this as is. If someone wants to send a PR with |
|
anyone has any objections to calling the step and option a more explicit: expect_status: 200..299? I think it plays nicer which how I ended up calling the exception: Req.UnexpectedResponseStatusError. Could see shorter Req.UnexpectedStausError too, btw. The main driver is there’s an so :expect_status would totally disambiguate even if slightly more verbose. thoughts? |
|
@wojtekmach I rather it call it
|
This is a replacement for https://hexdocs.pm/req/Req.Steps.html#handle_http_errors/1 which will be deprecated and removed in Req 1.0.
Some more context here:
handle_http_errors: Support:responseand:exception#193In a nutshell: