Method: Aws::AWSErrorHandler#check

Defined in:
lib/awsbase/errors.rb

#check(request, options = {}) ⇒ Object

Returns false if



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/awsbase/errors.rb', line 185

def check(request, options={}) #:nodoc:
  result            = false
  error_found       = false
  redirect_detected = false
  error_match       = nil
  last_errors_text  = ''
  response          = @aws.last_response
  # log error
  request_text_data = "#{request[:server]}:#{request[:port]}#{request[:request].path}"
  # is this a redirect?
  # yes!
  if response.is_a?(Net::HTTPRedirection)
    redirect_detected = true
  else
    # no, it's an error ...
    @aws.logger.warn("##### #{@aws.class.name} returned an error: #{response.code} #{response.message}\n#{response.body} #####")
    @aws.logger.warn("##### #{@aws.class.name} request: #{request_text_data} ####")
  end
  # Check response body: if it is an Amazon XML document or not:
  if redirect_detected || (response.body && response.body[/<\?xml/]) # ... it is a xml document
    @aws.class.bench_xml.add! do
      error_parser = RightErrorResponseParser.new
      error_parser.parse(response)
      @aws.last_errors     = error_parser.errors
      @aws.last_request_id = error_parser.requestID
      last_errors_text     = @aws.last_errors.flatten.join("\n")
      # on redirect :
      if redirect_detected
        location = response['location']
        # ... log information and ...
        @aws.logger.info("##### #{@aws.class.name} redirect requested: #{response.code} #{response.message} #####")
        @aws.logger.info("##### New location: #{location} #####")
        # ... fix the connection data
        request[:server]   = URI.parse(location).host
        request[:protocol] = URI.parse(location).scheme
        request[:port]     = URI.parse(location).port
      end
    end
  else # ... it is not a xml document(probably just a html page?)
    @aws.last_errors     = [[response.code, "#{response.message} (#{request_text_data})"]]
    @aws.last_request_id = '-undefined-'
    last_errors_text     = response.message
  end
  # now - check the error
  unless redirect_detected
    @errors_list.each do |error_to_find|
      if last_errors_text[/#{error_to_find}/i]
        error_found = true
        error_match = error_to_find
        @aws.logger.warn("##### Retry is needed, error pattern match: #{error_to_find} #####")
        break
      end
    end
  end
  # check the time has gone from the first error come
  if redirect_detected || error_found
    # Close the connection to the server and recreate a new one.
    # It may have a chance that one server is a semi-down and reconnection
    # will help us to connect to the other server
    if !redirect_detected && @close_on_error
      @aws.connection.finish "#{self.class.name}: error match to pattern '#{error_match}'"
    end
# puts 'OPTIONS3=' + options.inspect
    if options[:retries].nil? || @retries < options[:retries]
      if (Time.now < @stop_at)
        @retries += 1
        unless redirect_detected
          @aws.logger.warn("##### Retry ##{@retries} is being performed. Sleeping for #{@reiteration_delay} sec. Whole time: #{Time.now-@started_at} sec ####")
          sleep @reiteration_delay
          @reiteration_delay *= 2

          # Always make sure that the fp is set to point to the beginning(?)
          # of the File/IO. TODO: it assumes that offset is 0, which is bad.
          if (request[:request].body_stream && request[:request].body_stream.respond_to?(:pos))
            begin
              request[:request].body_stream.pos = 0
            rescue Exception => e
              @logger.warn("Retry may fail due to unable to reset the file pointer" +
                               " -- #{self.class.name} : #{e.inspect}")
            end
          end
        else
          @aws.logger.info("##### Retry ##{@retries} is being performed due to a redirect.  ####")
        end
        result = @aws.request_info(request, @parser, options)
      else
        @aws.logger.warn("##### Ooops, time is over... ####")
      end
    else
      @aws.logger.info("##### Stopped retrying because retries=#{@retries} and max=#{options[:retries]}  ####")
    end
    # aha, this is unhandled error:
  elsif @close_on_error
    # Is this a 5xx error ?
    if @aws.last_response.code.to_s[/^5\d\d$/]
      @aws.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}'"
      # Is this a 4xx error ?
    elsif @aws.last_response.code.to_s[/^4\d\d$/] && @close_on_4xx_probability > rand(100)
      @aws.connection.finish "#{self.class.name}: code: #{@aws.last_response.code}: '#{@aws.last_response.message}', " +
                                 "probability: #{@close_on_4xx_probability}%"
    end
  end
  result
end