@@ -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