-
Notifications
You must be signed in to change notification settings - Fork 341
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
Do not raise errors on failure, but return an HTTPError struct #27
Comments
Would this use |
I was thinking on just structs as they can be easily pattern matched and have something like this: case HTTPoison.get(url) do
%HTTPoison.Response{status_code: 200, body: body} ->
IO.puts body
%HTTPoison.Response{status_code: 404} ->
IO.puts "Not found :("
%HTTPoison.Error(reason: reason) ->
IO.puts "Error during request: #{inspect reason}"
end |
Seems reasonable. I definitely prefer this than having to catch an error, and it's more idiomatic. |
+1 for this, I implemented retries with exponential back-off in my simpledb library this week and my implementation would've been cleaner if I didn't have to handle both 500 range responses and rescue exceptions as well. (The simpledb API hits httpoison/lib/httpoison/base.ex Line 89 in 6d8b6aa
I did a Response or Error struct return value in my Zencoder library https://github.com/zencoder/zencoder-ex and an :ok or :error tuple situation for my simpledb library https://github.com/zencoder/zencoder-ex and having done it both ways I really haven't developed a preference for one or the other. |
I cam here to open this exact issue. You can go with |
@josevalim, yeah I see your point. I was writing some examples of usage yesterday, and the good thing about the {:ok, response} -> IO.puts response.body To do it with just the struct, the code would look like this: %HTTPoison.Response{} = response -> IO.puts response.body I don't love the idea of bang methods but it's a nice way to people change and follow the old behaviour. The other thing to think is the error tuple, should it be
The simples solution I see is use the fact that exception is a struct and use it everywhere I need to represent an error. |
It is ok to return |
I just pushed the changes discussed here: 86471f9 Any feedback is welcome! If everything is fine I'll release 0.5.0 tomorrow! |
👍 |
exception anymore. Reference: edgurgel/httpoison#27
This would be a breaking change on the current API.
No more raising errors, just a struct. This change would make pattern matching easier and follow the way that most Erlang/Elixir APIs work.
Related: #26
The text was updated successfully, but these errors were encountered: