[Ruby] Make connection failures visible with generated Ruby SDKs#3640
Merged
wing328 merged 2 commits intoswagger-api:masterfrom Aug 30, 2016
Merged
[Ruby] Make connection failures visible with generated Ruby SDKs#3640wing328 merged 2 commits intoswagger-api:masterfrom
wing328 merged 2 commits intoswagger-api:masterfrom
Conversation
| fail ApiError.new('Connection timed out') | ||
| elsif response.code == 0 | ||
| # Errors from libcurl will be made visible here | ||
| fail ApiError.new(response.return_message) |
Contributor
There was a problem hiding this comment.
Given that the response.code is 0, what about creating the ApiError with ApiError.new(:code => 0, :message => response.return_message) instead?
Contributor
Author
There was a problem hiding this comment.
Good suggestion. Updated.
The underlying HTTP library, Typhoeus, requires you to be explicit about error handling. Unfortunately, this also means that we can't assume that `response.success?` will be false only when the HTTP status code is not a 200; it could also be false when the request fails (timeouts, TLS verification issues, etc.). This commit adds explicit error handling for these cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make HTTP connection failures (e.g. timeouts and TLS verification failures) visible.
I initially started out by filing an issue before figuring out how I could actually resolve this. I pasted the relevant parts of that issue below.
(For what it's worth, I discovered this while updating our SDK to use more up-to-date templates. Here's the branch I'm working on if it makes things easier.)
Current behavior
To reproduce the issue of connection timeout:
I modified this section of code slightly to see how I could resolve that above issue. It looks like this:
And for some reason, it will always pick the second branch and give out an empty error message.
To reproduce the second issue (TLS verification), using above modification:
There is no obvious indication of what went wrong.
[1]:
/usr/local/opt/openssl/bin/openssl s_server -accept 9876 -cert ../server.cert -key ../server.key -WWW(using Homebrewed OpenSSL because the OS X-provided OpenSSL is old and deprecated and supports effectively no modern TLS protocols)Modified behavior from this pull request
Disconnected from network:
TLS verification:
Follow-up question
I'm curious about why we chose to use Typhoeus as the underlying HTTP library. I spoke with a few Rubyist colleagues here, and here's how the Slack conversation turned out (my personal expressed opinions might not be fair because I haven't used the library much):
I did a quick search and it looks like the generated SDK only uses Typhoeus via
Typhoeus::Request.new. It don't use any of that fancy job queuing stuff likeTyphoeus::Hydra. As for why I think Typhoeus's interface is awful, it's pretty much because I had to make this fix: the library seems opinionated about not raising errors, so you have to be very explicit about handling HTTP errors or else they'll pretty much get dropped to the floor.