-
Notifications
You must be signed in to change notification settings - Fork 158
New step: expect
#493
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
base: main
Are you sure you want to change the base?
New step: expect
#493
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)
| @@ -0,0 +1,16 @@ | |||
| defmodule Req.UnexpectedResponseError do | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another idea for calling this:
| defmodule Req.UnexpectedResponseError do | |
| defmodule Req.UnexpectedResponseStatusError do |
|
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect: :success looks pretty good to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 🫠 |
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: