Exception: Rapidash::ResponseError
- Inherits:
-
StandardError
- Object
- StandardError
- Rapidash::ResponseError
- Defined in:
- lib/rapidash/response_error.rb
Overview
Rapidash::ResponseError Exception that gets raised if the response is an error (4xx or 5xx) Raised by Faraday::Response::RaiseRapidashError see lib/faraday/response/raise_rapidash_error.rb
Formats a readable error message including HTTP status, method and requested URL
Examples:
client.posts.create!(title: '')
Rapidash::ResponseError: 422 POST http://acme.com/api/v1/posts.json
begin
client.posts.create!(title: '')
rescue Rapidash::ResponseError => response
response.status # => 422
response.method # => "POST"
response.url # => "http://acme.com/api/v1/posts.json"
response.body # => "{"errors":["title can't be blank", "body can't be blank"]}"
end
Hint: Can be easily sub-classed to provide a custom exception handler class with specific error formatting. Defining an ‘errors` method that returns a String or Array will include the errors in the exception message:
class MyCustomResponseError < Rapidash::ResponseError
def errors
data = JSON.parse(body)
data['errors']
end
end
Rapidash.response_exception_class = MyCustomResponseError
client.posts.create!(title: '')
MyCustomResponseError: 422 POST http://acme.com/api/v1/posts.json | Errors: title can't be blank, body can't be blank
begin
client.posts.create!(title: '')
rescue Rapidash::ResponseError => response
response.status # => 422
response.errors # => ["title can't be blank", "body can't be blank"]
end
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(response = nil) ⇒ ResponseError
constructor
A new instance of ResponseError.
- #to_s ⇒ Object
Constructor Details
#initialize(response = nil) ⇒ ResponseError
Returns a new instance of ResponseError.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rapidash/response_error.rb', line 50 def initialize(response = nil) @response = response @body = response[:body] @status = response[:status].to_i @method = response[:method].to_s.upcase @url = response[:url] super end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
48 49 50 |
# File 'lib/rapidash/response_error.rb', line 48 def body @body end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
48 49 50 |
# File 'lib/rapidash/response_error.rb', line 48 def method @method end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
48 49 50 |
# File 'lib/rapidash/response_error.rb', line 48 def response @response end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
48 49 50 |
# File 'lib/rapidash/response_error.rb', line 48 def status @status end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
48 49 50 |
# File 'lib/rapidash/response_error.rb', line 48 def url @url end |
Instance Method Details
#to_s ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rapidash/response_error.rb', line 61 def to_s msg = "#{status} #{method} #{url}" if respond_to?(:errors) && !(errors.blank?) errors.map(&:to_s).join(', ') if errors.kind_of?(Array) msg = "#{msg} | Errors: #{errors.to_s}" end msg end |