Skip to content

Commit 741f43e

Browse files
committed
Merge pull request #112 from intercom/BL/timeout-enhance
Raise a ServiceConnectionError.new error when connection fails
2 parents 02d43c7 + da6fbde commit 741f43e

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ else rescue the more specific error subclass.
337337
Intercom::AuthenticationError
338338
Intercom::ServerError
339339
Intercom::ServiceUnavailableError
340+
Intercom::ServiceConnectionError
340341
Intercom::ResourceNotFound
341342
Intercom::BadRequestError
342343
Intercom::RateLimitExceeded

lib/intercom/errors.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ class ServerError < IntercomError; end
2020
# Raised when we have bad gateway errors.
2121
class BadGatewayError < IntercomError; end
2222

23-
# Raised when we reach socket connect timeout
23+
# Raised when we experience a socket read timeout
2424
class ServiceUnavailableError < IntercomError; end
2525

26+
# Raised when we experience socket connect timeout
27+
class ServiceConnectionError < IntercomError; end
28+
2629
# Raised when requesting resources on behalf of a user that doesn't exist in your application on Intercom.
2730
class ResourceNotFound < IntercomError; end
2831

lib/intercom/request.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,24 @@ def client(uri)
5757
def execute(target_base_url=nil)
5858
base_uri = URI.parse(target_base_url)
5959
set_common_headers(net_http_method, base_uri)
60-
client(base_uri).start do |http|
61-
response = http.request(net_http_method)
62-
decoded = decode(response['content-encoding'], response.body)
63-
unless decoded.strip.empty?
64-
parsed_body = JSON.parse(decoded)
65-
raise_application_errors_on_failure(parsed_body, response.code.to_i) if parsed_body['type'] == 'error.list'
60+
begin
61+
client(base_uri).start do |http|
62+
begin
63+
response = http.request(net_http_method)
64+
decoded = decode(response['content-encoding'], response.body)
65+
unless decoded.strip.empty?
66+
parsed_body = JSON.parse(decoded)
67+
raise_application_errors_on_failure(parsed_body, response.code.to_i) if parsed_body['type'] == 'error.list'
68+
end
69+
raise_errors_on_failure(response)
70+
parsed_body
71+
rescue Timeout::Error
72+
raise Intercom::ServiceUnavailableError.new('Service Unavailable [request timed out]')
73+
end
6674
end
67-
raise_errors_on_failure(response)
68-
parsed_body
75+
rescue Timeout::Error
76+
raise Intercom::ServiceConnectionError.new('Failed to connect to service [connection attempt timed out]')
6977
end
70-
rescue Timeout::Error
71-
raise Intercom::ServiceUnavailableError.new('Service Unavailable')
7278
end
7379

7480
def decode(content_encoding, body)

0 commit comments

Comments
 (0)