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.



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

Parameters:

  • object (Object)

    response object to process for errors

Returns:

  • (Boolean)


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

Parameters:

  • body (Hash)

    errors



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

Parameters:

  • array (Array)

    array to iterate



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

Parameters:

  • hash (Hash)

    hash to iterate



69
70
71
72
73
# File 'lib/bwapi/error.rb', line 69

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)


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

Parameters:

  • object (Object)

    type to determine



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