102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/restful_client.rb', line 102
def run_request(request, method, retry_if_needed)
logger.debug { "#{__method__} :: Request :: #{request.inspect}" }
request.options[:headers].merge!('X-Forwarded-For' => $client_ip) if $client_ip
request.options[:headers].merge!('User-Agent' => user_agent)
request.on_complete do |response|
if response.success?
logger.debug { "Success in #{method} :: Code: #{response.response_code}, #{response.body}" }
return '' if response.body.empty? || response.body == ' '
begin
return JSON.parse(response.body)
rescue => e
logger.error { "Response from #{response.effective_url} is not a valid json - [#{response.body}]" }
raise e
end
elsif response.timed_out?
@@timeout_occured_count += 1
skip_raise = (retry_if_needed && @@timeout_occured_count <= retries)
error_type = 'TimeoutOccured'
error_description = prettify_logger(error_type, request, response)
logger.error { "Time out in #{method} for: #{error_description}" }
exception = RuntimeError.new(error_description)
exception.set_backtrace(caller)
report_method.call('RestError', error_description, exception)
fail RestError.new(response.return_code.to_sym) unless skip_raise
elsif response.code == 0
error_type = :HttpError
error_description = prettify_logger(error_type, request, response)
logger.error { "HttpError Error #{response.code}/#{response.return_code} for: #{error_description}" }
exception = RuntimeError.new(error_description)
exception.set_backtrace(caller)
report_method.call('RestError', error_description, exception)
fail RestError.new(error_type)
elsif response.code >= SERVER_SIDE_ERRORS_RANGE
error_type = :BadReturnCode
error_description = prettify_logger(error_type, request, response)
logger.error { "#{error_type} #{response.code}/#{response.return_code} for: #{error_description}" }
exception = RuntimeError.new(error_description)
exception.set_backtrace(caller)
report_method.call('RestError', error_description, exception)
fail RestError.new(error_type)
elsif response.code.between?(CLIENT_SIDE_ERRORS_RANGE, (SERVER_SIDE_ERRORS_RANGE - 1))
logger.error { "#{error_type} #{response.code}/#{response.return_code} for: #{error_description}" }
return ''
else
fail RestError.new(:BadReturnCode)
end
end
request.run
end
|