Make connection failures visible with generated Ruby SDKs (#3640)

* Make connection failures visible in Ruby SDK

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.

* Update samples
This commit is contained in:
Daniel Ge
2016-08-29 19:28:07 -07:00
committed by wing328
parent 224fd208e7
commit dd1ed12318
7 changed files with 67 additions and 43 deletions

View File

@@ -47,10 +47,18 @@ module {{moduleName}}
end
unless response.success?
fail ApiError.new(:code => response.code,
:response_headers => response.headers,
:response_body => response.body),
response.status_message
if response.timed_out?
fail ApiError.new('Connection timed out')
elsif response.code == 0
# Errors from libcurl will be made visible here
fail ApiError.new(:code => 0,
:message => response.return_message)
else
fail ApiError.new(:code => response.code,
:response_headers => response.headers,
:response_body => response.body),
response.status_message
end
end
if opts[:return_type]

View File

@@ -8,7 +8,7 @@ This SDK is automatically generated by the [Swagger Codegen](https://github.com/
- API version: 1.0.0 */ ' \" =_end -- \\r\\n \\n \\r
- Package version: 1.0.0
- Build date: 2016-07-14T18:21:54.437+08:00
- Build date: 2016-08-29T14:24:34.432-07:00
- Build package: class io.swagger.codegen.languages.RubyClientCodegen
## Installation
@@ -87,18 +87,18 @@ Class | Method | HTTP request | Description
## Documentation for Authorization
### api_key
- **Type**: API key
- **API key parameter name**: api_key */ ' " =end -- \r\n \n \r
- **Location**: HTTP header
### petstore_auth
- **Type**: OAuth
- **Flow**: implicit
- **Authorization URL**: http://petstore.swagger.io/api/oauth/dialog
- **Scopes**:
- write:pets: modify pets in your account */ ' " =end -- \r\n \n \r
- read:pets: read your pets */ ' " =end -- \r\n \n \r
- write:pets: modify pets in your account */ ' \" =_end -- \\r\\n \\n \\r
- read:pets: read your pets */ ' \" =_end -- \\r\\n \\n \\r
### api_key
- **Type**: API key
- **API key parameter name**: api_key */ ' " =end -- \r\n \n \r
- **Location**: HTTP header

View File

@@ -66,10 +66,18 @@ module Petstore
end
unless response.success?
fail ApiError.new(:code => response.code,
:response_headers => response.headers,
:response_body => response.body),
response.status_message
if response.timed_out?
fail ApiError.new('Connection timed out')
elsif response.code == 0
# Errors from libcurl will be made visible here
fail ApiError.new(:code => 0,
:message => response.return_message)
else
fail ApiError.new(:code => response.code,
:response_headers => response.headers,
:response_body => response.body),
response.status_message
end
end
if opts[:return_type]
@@ -288,7 +296,7 @@ module Petstore
# Update hearder and query params based on authentication settings.
#
# @param [Hash] header_params Header parameters
# @param [Hash] form_params Query parameters
# @param [Hash] query_params Query parameters
# @param [String] auth_names Authentication scheme name
def update_params_for_auth!(header_params, query_params, auth_names)
Array(auth_names).each do |auth_name|

View File

@@ -201,13 +201,6 @@ module Petstore
# Returns Auth Settings hash for api client.
def auth_settings
{
'api_key' =>
{
type: 'api_key',
in: 'header',
key: 'api_key */ ' " =end -- \r\n \n \r',
value: api_key_with_prefix('api_key */ ' " =end -- \r\n \n \r')
},
'petstore_auth' =>
{
type: 'oauth2',
@@ -215,6 +208,13 @@ module Petstore
key: 'Authorization',
value: "Bearer #{access_token}"
},
'api_key' =>
{
type: 'api_key',
in: 'header',
key: 'api_key */ ' " =end -- \r\n \n \r',
value: api_key_with_prefix('api_key */ ' " =end -- \r\n \n \r')
},
}
end
end

View File

@@ -8,7 +8,7 @@ This SDK is automatically generated by the [Swagger Codegen](https://github.com/
- API version: 1.0.0
- Package version: 1.0.0
- Build date: 2016-08-22T16:46:39.641+08:00
- Build date: 2016-08-29T14:24:29.182-07:00
- Build package: class io.swagger.codegen.languages.RubyClientCodegen
## Installation
@@ -137,12 +137,6 @@ Class | Method | HTTP request | Description
## Documentation for Authorization
### api_key
- **Type**: API key
- **API key parameter name**: api_key
- **Location**: HTTP header
### petstore_auth
- **Type**: OAuth
@@ -156,3 +150,9 @@ Class | Method | HTTP request | Description
- **Type**: HTTP basic authentication
### api_key
- **Type**: API key
- **API key parameter name**: api_key
- **Location**: HTTP header

View File

@@ -66,10 +66,18 @@ module Petstore
end
unless response.success?
fail ApiError.new(:code => response.code,
:response_headers => response.headers,
:response_body => response.body),
response.status_message
if response.timed_out?
fail ApiError.new('Connection timed out')
elsif response.code == 0
# Errors from libcurl will be made visible here
fail ApiError.new(:code => 0,
:message => response.return_message)
else
fail ApiError.new(:code => response.code,
:response_headers => response.headers,
:response_body => response.body),
response.status_message
end
end
if opts[:return_type]

View File

@@ -201,13 +201,6 @@ module Petstore
# Returns Auth Settings hash for api client.
def auth_settings
{
'api_key' =>
{
type: 'api_key',
in: 'header',
key: 'api_key',
value: api_key_with_prefix('api_key')
},
'petstore_auth' =>
{
type: 'oauth2',
@@ -222,6 +215,13 @@ module Petstore
key: 'Authorization',
value: basic_auth_token
},
'api_key' =>
{
type: 'api_key',
in: 'header',
key: 'api_key',
value: api_key_with_prefix('api_key')
},
}
end
end