Skip to content
This repository was archived by the owner on Sep 5, 2019. It is now read-only.

Commit 370d1f9

Browse files
authored
Merge pull request #40 from boblail/define-two-new-exception-classes
Define exception classes for 503 and 504 responses
2 parents 134bc26 + 91b0817 commit 370d1f9

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

lib/swiftype/exceptions.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ class InvalidCredentials < ClientException; end
66
class BadRequest < ClientException; end
77
class Forbidden < ClientException; end
88
class UnexpectedHTTPException < ClientException; end
9+
10+
class ServerException < StandardError; end
11+
class InternalServerError < ServerException; end
12+
class BadGateway < ServerException; end
13+
class ServiceUnavailable < ServerException; end
14+
class GatewayTimeout < ServerException; end
915
end

lib/swiftype/request.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,29 @@ def handle_errors(response)
8989
case response
9090
when Net::HTTPSuccess
9191
response
92-
when Net::HTTPUnauthorized
93-
raise Swiftype::InvalidCredentials, error_message_from_response(response)
94-
when Net::HTTPNotFound
95-
raise Swiftype::NonExistentRecord, error_message_from_response(response)
96-
when Net::HTTPConflict
97-
raise Swiftype::RecordAlreadyExists, error_message_from_response(response)
98-
when Net::HTTPBadRequest
99-
raise Swiftype::BadRequest, error_message_from_response(response)
100-
when Net::HTTPForbidden
101-
raise Swiftype::Forbidden, error_message_from_response(response)
10292
else
93+
EXCEPTION_MAP.each do |response_class, exception_class|
94+
if response.is_a?(response_class)
95+
raise exception_class, error_message_from_response(response)
96+
end
97+
end
98+
10399
raise Swiftype::UnexpectedHTTPException, "#{response.code} #{response.body}"
104100
end
105101
end
106102

103+
EXCEPTION_MAP = {
104+
Net::HTTPUnauthorized => Swiftype::InvalidCredentials,
105+
Net::HTTPNotFound => Swiftype::NonExistentRecord,
106+
Net::HTTPConflict => Swiftype::RecordAlreadyExists,
107+
Net::HTTPBadRequest => Swiftype::BadRequest,
108+
Net::HTTPForbidden => Swiftype::Forbidden,
109+
Net::HTTPInternalServerError => Swiftype::InternalServerError,
110+
Net::HTTPBadGateway => Swiftype::BadGateway,
111+
Net::HTTPServiceUnavailable => Swiftype::ServiceUnavailable,
112+
Net::HTTPGatewayTimeOut => Swiftype::GatewayTimeout
113+
}.freeze
114+
107115
def error_message_from_response(response)
108116
body = response.body
109117
json = JSON.parse(body) if body && body.strip != ''

0 commit comments

Comments
 (0)