Exception: BWAPI::BWError
- Inherits:
-
StandardError
- Object
- StandardError
- BWAPI::BWError
- Defined in:
- lib/bwapi/error.rb
Overview
BW error class to capture BWAPI error responses
Direct Known Subclasses
BadGateway, BadRequest, Forbidden, InternalServerError, NotAcceptable, NotFound, NotImplemented, ServiceUnavailable, TooManyRequests, Unauthorized, UnprocessableEntity
Instance Method Summary collapse
-
#errors_keys?(body) ⇒ Boolean
Check if response has known errors keys.
-
#initialize(response = nil) ⇒ BWError
constructor
A new instance of BWError.
-
#parse_errors(body) ⇒ Object
Parses errors based on error body passed.
-
#split_array_errors(array) ⇒ Object
Iterates through errors in array.
-
#split_hash_errors(hash) ⇒ Object
Iterates through errors in hash.
-
#valid_response?(response) ⇒ Boolean
Check if response is valid.
-
#verify_type(type) ⇒ Object
Verifies type.
Constructor Details
#initialize(response = nil) ⇒ BWError
Returns a new instance of BWError.
4 5 6 7 8 |
# File 'lib/bwapi/error.rb', line 4 def initialize(response = nil) @errors = [] valid_response?(response) @errors.empty? ? super() : super(@errors.join(', ')) end |
Instance Method Details
#errors_keys?(body) ⇒ Boolean
Check if response has known errors keys
23 24 25 26 27 28 29 |
# File 'lib/bwapi/error.rb', line 23 def errors_keys?(body) if body.key?('error') && body.key?('error_description') body elsif body.key?('errors') body['errors'] end end |
#parse_errors(body) ⇒ Object
Parses errors based on error body passed
34 35 36 |
# File 'lib/bwapi/error.rb', line 34 def parse_errors(body) verify_type body end |
#split_array_errors(array) ⇒ Object
Iterates through errors in array
55 56 57 |
# File 'lib/bwapi/error.rb', line 55 def split_array_errors(array) array.each_with_index { |_e, i| verify_type array[i] } end |
#split_hash_errors(hash) ⇒ Object
Iterates through errors in hash
62 63 64 65 66 |
# File 'lib/bwapi/error.rb', line 62 def split_hash_errors(hash) = [] hash.each { |k, v| << "#{k}: #{v}" } @errors << .flatten.join(' with ') end |
#valid_response?(response) ⇒ Boolean
Check if response is valid
13 14 15 16 17 18 |
# File 'lib/bwapi/error.rb', line 13 def valid_response?(response) return nil if response.nil? return nil unless response.body.is_a?(Object) && response.respond_to?(:body) return nil unless response.body.is_a?(Hash) parse_errors(errors_keys?(response.body)) unless response.body.nil? end |
#verify_type(type) ⇒ Object
Verifies type
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bwapi/error.rb', line 41 def verify_type(type) case type when Array split_array_errors(type) when Hash split_hash_errors(type) when String @errors << type end end |