Class: Rets::Client::ErrorChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/rets/client.rb

Class Method Summary collapse

Class Method Details

.check(response) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/rets/client.rb', line 349

def self.check(response)
  # some RETS servers returns HTTP code 412 when session cookie expired, yet the response body
  # passes XML check. We need to special case for this situation.
  # This method is also called from multipart.rb where there are headers and body but no status_code
  if response.respond_to?(:status_code) && response.status_code == 412
    raise HttpError, "HTTP status: #{response.status_code}, body: #{response.body}"
  end

  # some RETS servers return success code in XML body but failure code 4xx in http status
  # If xml body is present we ignore http status

  if !response.body.empty?
    begin
      xml = Nokogiri::XML.parse(response.body, nil, nil, Nokogiri::XML::ParseOptions::STRICT)

      rets_element = xml.xpath("/RETS")
      if rets_element.empty?
        return
      end
      reply_text = (rets_element.attr("ReplyText") || rets_element.attr("replyText")).value
      reply_code = (rets_element.attr("ReplyCode") || rets_element.attr("replyCode")).value.to_i

      if reply_code.nonzero?
        raise InvalidRequest.new(reply_code, reply_text)
      else
        return
      end
    rescue Nokogiri::XML::SyntaxError
      #Not xml
    end
  end

  if response.respond_to?(:ok?) && ! response.ok?
    if response.status_code == 401
      raise AuthorizationFailure.new(response.status_code, response.body)
    else
      raise HttpError, "HTTP status: #{response.status_code}, body: #{response.body}"
    end
  end
end