Exception: Azure::Core::Http::HTTPError

Inherits:
Error
  • Object
show all
Defined in:
lib/azure/core/http/http_error.rb

Overview

Public: Class for handling all HTTP response errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ HTTPError

Public: Initialize an error

http_response - An Azure::Core::HttpResponse



70
71
72
73
74
75
76
77
78
# File 'lib/azure/core/http/http_error.rb', line 70

def initialize(http_response)
  @http_response = http_response
  @uri = http_response.uri
  @status_code = http_response.status_code
  parse_response
  # Use reason phrase as the description if description is empty
  @description = http_response.reason_phrase if (@description.nil? || @description.empty?) && http_response.reason_phrase
  super("#{type} (#{status_code}): #{description}")
end

Instance Attribute Details

#descriptionObject (readonly)

Public: Description of the error

Returns a String



50
51
52
# File 'lib/azure/core/http/http_error.rb', line 50

def description
  @description
end

#detailObject (readonly)

Public: Detail of the error

Returns a String



55
56
57
# File 'lib/azure/core/http/http_error.rb', line 55

def detail
  @detail
end

#headerObject (readonly)

Public: The header name whose value is invalid

Returns a String



60
61
62
# File 'lib/azure/core/http/http_error.rb', line 60

def header
  @header
end

#header_valueObject (readonly)

Public: The invalid header value

Returns a String



65
66
67
# File 'lib/azure/core/http/http_error.rb', line 65

def header_value
  @header_value
end

#http_responseObject (readonly)

Public: Detail of the response

Returns an Azure::Core::Http::HttpResponse object



28
29
30
# File 'lib/azure/core/http/http_error.rb', line 28

def http_response
  @http_response
end

#status_codeObject (readonly)

Public: The HTTP status code of this error

Returns a Fixnum



38
39
40
# File 'lib/azure/core/http/http_error.rb', line 38

def status_code
  @status_code
end

#typeObject (readonly)

Public: The type of error

msdn.microsoft.com/en-us/library/azure/dd179357

Returns a String



45
46
47
# File 'lib/azure/core/http/http_error.rb', line 45

def type
  @type
end

#uriObject (readonly)

Public: The request URI

Returns a String



33
34
35
# File 'lib/azure/core/http/http_error.rb', line 33

def uri
  @uri
end

Instance Method Details

#inspectObject



127
128
129
130
131
# File 'lib/azure/core/http/http_error.rb', line 127

def inspect
  string = "#<#{self.class.name}:#{self.object_id} "
  fields = self.instance_variables.map{|field| "#{field}: #{self.send(field.to_s.delete("@")).inspect}"}
  string << fields.join(", ") << ">"
end

#parse_json_responseObject



114
115
116
117
118
# File 'lib/azure/core/http/http_error.rb', line 114

def parse_json_response
  odata_error = JSON.parse(@http_response.body)['odata.error']
  @type = odata_error['code']
  @description = odata_error['message']['value']
end

#parse_responseObject

Extract the relevant information from the response’s body. If the response body is not an XML, we return an ‘Unknown’ error with the entire body as the description

Returns nothing



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/azure/core/http/http_error.rb', line 85

def parse_response
  if @http_response.body && @http_response.respond_to?(:headers) && @http_response.headers['Content-Type']
    if @http_response.headers['Content-Type'].include?('xml')
      parse_xml_response
    elsif @http_response.headers['Content-Type'].include?('json')
      parse_json_response
    end
  else
    parse_unknown_response
  end
end

#parse_unknown_responseObject



120
121
122
123
124
125
# File 'lib/azure/core/http/http_error.rb', line 120

def parse_unknown_response
  @type = 'Unknown'
  if @http_response.body
    @description = "#{@http_response.body.strip}"
  end
end

#parse_xml_responseObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/azure/core/http/http_error.rb', line 97

def parse_xml_response
  document = Nokogiri.Slop(@http_response.body)

  @type = document.css('code').first.text if document.css('code').any?
  @type = document.css('Code').first.text if document.css('Code').any?
  @description = document.css('message').first.text if document.css('message').any?
  @description = document.css('Message').first.text if document.css('Message').any?
  @header = document.css('headername').first.text if document.css('headername').any?
  @header = document.css('HeaderName').first.text if document.css('HeaderName').any?
  @header_value = document.css('headervalue').first.text if document.css('headervalue').any?
  @header_value = document.css('HeaderValue').first.text if document.css('HeaderValue').any?

  # service bus uses detail instead of message
  @detail = document.css('detail').first.text if document.css('detail').any?
  @detail = document.css('Detail').first.text if document.css('Detail').any?
end