Exception: BWAPI::BWError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/bwapi/error.rb

Overview

BW error class to capture BWAPI error responses

Instance Method Summary collapse

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

Parameters:

  • object (Object)

    response object to process for errors

Returns:

  • (Boolean)


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

Parameters:

  • body (Hash)

    errors



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

Parameters:

  • array (Array)

    array to iterate



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

Parameters:

  • hash (Hash)

    hash to iterate



62
63
64
65
66
# File 'lib/bwapi/error.rb', line 62

def split_hash_errors(hash)
  message = []
  hash.each { |k, v| message << "#{k}: #{v}" }
  @errors << message.flatten.join(' with ')
end

#valid_response?(response) ⇒ Boolean

Check if response is valid

Parameters:

  • object (Object)

    response object to check for errors

Returns:

  • (Boolean)


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

Parameters:

  • object (Object)

    type to determine



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