Exception: X::HTTPError
- Defined in:
- lib/x/errors/http_error.rb
Direct Known Subclasses
Constant Summary collapse
- JSON_CONTENT_TYPE_REGEXP =
%r{application/(problem\+|)json}
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #error_message(response) ⇒ Object
-
#initialize(response:) ⇒ HTTPError
constructor
A new instance of HTTPError.
- #json?(response) ⇒ Boolean
- #message_from_json_response(response) ⇒ Object
Constructor Details
#initialize(response:) ⇒ HTTPError
Returns a new instance of HTTPError.
10 11 12 13 14 |
# File 'lib/x/errors/http_error.rb', line 10 def initialize(response:) super((response)) @response = response @code = response.code end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
8 9 10 |
# File 'lib/x/errors/http_error.rb', line 8 def code @code end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
8 9 10 |
# File 'lib/x/errors/http_error.rb', line 8 def response @response end |
Instance Method Details
#error_message(response) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/x/errors/http_error.rb', line 16 def (response) if json?(response) (response) else response. end end |
#json?(response) ⇒ Boolean
37 38 39 |
# File 'lib/x/errors/http_error.rb', line 37 def json?(response) JSON_CONTENT_TYPE_REGEXP.match?(response["content-type"]) end |
#message_from_json_response(response) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/x/errors/http_error.rb', line 24 def (response) response_object = JSON.parse(response.body) if response_object.key?("title") && response_object.key?("detail") "#{response_object.fetch("title")}: #{response_object.fetch("detail")}" elsif response_object.key?("error") response_object.fetch("error") elsif response_object["errors"].instance_of?(Array) response_object.fetch("errors").map { |error| error.fetch("message") }.join(", ") else response. end end |