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.
6 7 8 9 10 |
# File 'lib/bwapi/error.rb', line 6 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
26 27 28 29 30 31 32 33 34 |
# File 'lib/bwapi/error.rb', line 26 def errors_keys?(body) if body.error? && body.error_description? body elsif body.errors? body.errors else nil end end |
#parse_errors(body) ⇒ Object
Parses errors based on error body passed
39 40 41 |
# File 'lib/bwapi/error.rb', line 39 def parse_errors(body) verify_type body end |
#split_array_errors(array) ⇒ Object
Iterates through errors in array
60 61 62 63 64 |
# File 'lib/bwapi/error.rb', line 60 def split_array_errors(array) array.each_with_index do |_e, i| verify_type array[i] end end |
#split_hash_errors(hash) ⇒ Object
Iterates through errors in hash
69 70 71 72 73 |
# File 'lib/bwapi/error.rb', line 69 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
15 16 17 18 19 20 21 |
# File 'lib/bwapi/error.rb', line 15 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?(Hashie::Mash) body = errors_keys?(response.body) parse_errors(body) unless body.nil? end |
#verify_type(type) ⇒ Object
Verifies type
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bwapi/error.rb', line 46 def verify_type(type) case type when Array split_array_errors type when Hash, Hashie::Mash split_hash_errors type when String @errors << type end end |