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

Do not raise errors on failure, but return an HTTPError struct #27

Closed
edgurgel opened this issue Oct 9, 2014 · 9 comments
Closed

Do not raise errors on failure, but return an HTTPError struct #27

edgurgel opened this issue Oct 9, 2014 · 9 comments

Comments

@edgurgel
Copy link
Owner

edgurgel commented Oct 9, 2014

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

@BlakeWilliams
Copy link
Contributor

Would this use { :ok, response } and { :error, error } or just directly return the structs?

@edgurgel
Copy link
Owner Author

edgurgel commented Oct 9, 2014

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

@BlakeWilliams
Copy link
Contributor

Seems reasonable. I definitely prefer this than having to catch an error, and it's more idiomatic.

@adamkittelson
Copy link

+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

_ -> raise HTTPoison.HTTPError, message: "Failed to fetch the body"
every once in a while).

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.

@edgurgel edgurgel changed the title Do not raise errors on failure, but return a HTTPError struct Do not raise errors on failure, but return an HTTPError struct Oct 12, 2014
@josevalim
Copy link

I cam here to open this exact issue. You can go with get not raising and get! raising. I would also advocate for the simple {:ok, _} and {:error, _} just because it is definitely more concise than matching on the whole struct (and closer to Elixir's codebase too).

@edgurgel
Copy link
Owner Author

@josevalim, yeah I see your point. I was writing some examples of usage yesterday, and the good thing about the ok tuple is that you can easily use the response this way too:

{: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 {error, reason} or {error, exception} or {error, error_struct}?. I don't like any of it.

The simples solution I see is use the fact that exception is a struct and use it everywhere I need to represent an error.

@josevalim
Copy link

It is ok to return {:error, struct}.

@edgurgel
Copy link
Owner Author

I just pushed the changes discussed here: 86471f9

Any feedback is welcome! If everything is fine I'll release 0.5.0 tomorrow!

@josevalim
Copy link

👍

moski added a commit to mojaz-io/expander that referenced this issue Aug 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants