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

Remove expand! by raising errors during expand #10

Merged
merged 1 commit into from
Jun 1, 2015
Merged

Conversation

mudge
Copy link
Contributor

@mudge mudge commented May 31, 2015

GitHub: #1

Rather than having separate expand and expand! methods (one swallowing all exceptions, one not), simplify the API of Embiggen::URI to a single expand method. This no longer aggressively swallows exceptions outside of its control but may raise subclasses of Embiggen::Error, namely:

  • Embiggen::BadShortenedURI;
  • Embiggen::TooManyRedirects;
  • Embiggen::NetworkError.

The last of these wraps any network-related exceptions including timeouts, connection resets, unreachable hosts, etc.

The reason for this change is that these truly are exceptional circumstances outside of the library's control (unlike, say, an unshortened URI being passed to Embiggen::URI which can be handled gracefully). The previous API took too much responsibility away from the client by silently discarding exceptions by default and therefore obscuring failures.

Originally, the idea of using a sum type was appealing (e.g. returning some sort of result object which could be interrogated for its success) but this later seemed overengineered and unidiomatic. Instead, we prefer to return standard Ruby types where appropriate (e.g. standard library URIs) and raise exceptions in case of failure.

This simplification lead to a refactoring that extracted an HttpClient class which is currently used privately within the library. This potentially opens the way for pluggable HTTP clients (rather than being forced to use Net::HTTP) in future (c.f. #9).

@mudge mudge changed the title Remove expand! by raising errors during expand Remove expand! by raising errors during expand May 31, 2015
@jbilbo
Copy link
Member

jbilbo commented Jun 1, 2015

looks good to me 👍

def extract_redirects(request_options = {})
redirects = request_options.fetch(:redirects) { Configuration.redirects }
fail TooManyRedirects.new(
"#{uri} redirected too many times", uri) if redirects.zero?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong, but at this point uri is not the original one anymore, so the message is not entirely correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true: it's the last URI followed.

Perhaps it's just the message that needs correcting here as the initial URI is in the original Embiggen::URI instance but it seems useful to show how much progress was made.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sth like "Following #{uri} reached the redirects limit"

@jakubpawlowicz
Copy link

Otherwise, looks good to me!

GitHub: #1

Rather than having separate expand and expand! methods (one swallowing
all exceptions, one not), simplify the API of Embiggen::URI to a single
expand method. This no longer aggressively swallows exceptions outside
of its control but may raise subclasses of Embiggen::Error, namely:

* Embiggen::BadShortenedURI;
* Embiggen::TooManyRedirects;
* Embiggen::NetworkError.

The last of these wraps any network-related exceptions including
timeouts, connection resets, unreachable hosts, etc.

The reason for this change is that these truly are exceptional
circumstances outside of the library's control (unlike, say, an
unshortened URI being passed to Embiggen::URI which can be handled
gracefully). The previous API took too much responsibility away from the
client by silently discarding exceptions by default and therefore
obscuring failures.

Originally, the idea of using a [sum type][0] was appealing (e.g.
returning some sort of result object which could be interrogated for its
success) but this later seemed overengineered and unidiomatic. Instead,
we prefer to return standard Ruby types where appropriate (e.g. standard
library URIs) and raise exceptions in case of failure.

This simplification lead to a refactoring that extracted an HttpClient
class which is currently used privately within the library. This
potentially opens the way for pluggable HTTP clients (rather than being
forced to use Net::HTTP) in future (c.f. #9).

[0]: http://en.wikipedia.org/wiki/Tagged_union
@mudge mudge merged commit dc90199 into 1.x Jun 1, 2015
@mudge mudge deleted the remove-expand-bang branch June 1, 2015 13:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants