Exception: Essential::APIError
- Inherits:
-
StandardError
- Object
- StandardError
- Essential::APIError
- Defined in:
- lib/essential/errors/api_error.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#http_status ⇒ Object
readonly
Returns the value of attribute http_status.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(http_status: nil, body: nil, response: nil) ⇒ APIError
constructor
A new instance of APIError.
- #to_s ⇒ Object
Constructor Details
#initialize(http_status: nil, body: nil, response: nil) ⇒ APIError
Returns a new instance of APIError.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/essential/errors/api_error.rb', line 26 def initialize(http_status: nil, body: nil, response: nil) @http_status = http_status @response = response if body begin @body = JSON.parse(body) if @body.key?('error') error = @body['error'] @type = error['type'] @message = error['message'] @code = error['code'] @params = error['params'] end rescue JSON::ParserError @body = body end end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/essential/errors/api_error.rb', line 5 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
6 7 8 |
# File 'lib/essential/errors/api_error.rb', line 6 def code @code end |
#http_status ⇒ Object (readonly)
Returns the value of attribute http_status.
5 6 7 |
# File 'lib/essential/errors/api_error.rb', line 5 def http_status @http_status end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
6 7 8 |
# File 'lib/essential/errors/api_error.rb', line 6 def @message end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
6 7 8 |
# File 'lib/essential/errors/api_error.rb', line 6 def params @params end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
5 6 7 |
# File 'lib/essential/errors/api_error.rb', line 5 def response @response end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/essential/errors/api_error.rb', line 6 def type @type end |
Class Method Details
.from_exception(e) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/essential/errors/api_error.rb', line 8 def self.from_exception(e) case e # when SocketError # when NoMethodError when RestClient::ExceptionWithResponse new( http_status: e.http_code, body: e.http_body, response: e.response ) when RestClient::Exception new(http_status: e.http_code) # when Errno::ECONNREFUSED else raise e end end |
Instance Method Details
#to_s ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/essential/errors/api_error.rb', line 46 def to_s parts = [] parts << format('<%s>', self.http_status) parts << self.type parts << self. if self. parts << self.params.to_json if self.params parts.join(' - ') end |