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.



5
6
7
8
9
# File 'lib/bwapi/error.rb', line 5

def initialize response = nil
  errors_object = determine_errors(response)
  super() if errors_object.nil?
  super(generate_error_messages(errors_object))
end

Instance Method Details

#determine_errors(response) ⇒ Object

Determines from response and returns error(s)



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bwapi/error.rb', line 12

def determine_errors response
  return nil if response.nil?
  return nil unless response.is_a?(Hash) && response.has_key?(:body)
  return nil unless response[:body].is_a?(Hash)

  # Determine if response body has known error keys and return
  if response[:body].has_key?('error') && response[:body].has_key?('error_description')
    return response[:body]
  elsif response[:body].has_key?('errors')
    return response[:body]['errors']
  else
    return nil
  end
end

#generate_error_messages(errors_object) ⇒ Object

Generates error messsages based on error object passed

Parameters:

  • errors_object (Hash) —

    errors



30
31
32
33
34
# File 'lib/bwapi/error.rb', line 30

def generate_error_messages errors_object
  @error_messages = []
  verify_object_class errors_object
  return @error_messages.join(', ')
end

#split_array_errors(array) ⇒ Object

Iterates through errors in array

Parameters:

  • array (Array) —

    array to iterate



53
54
55
56
57
# File 'lib/bwapi/error.rb', line 53

def split_array_errors array
  array.each_with_index do |e, i|
    verify_object_class array[i]
  end
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 << "%s: %s" % [k, v]}
  @error_messages << message.flatten.join(' with ')
end

#verify_object_class(object) ⇒ Object

Verifies objects class

Parameters:

  • object (Object) —

    object to determine



39
40
41
42
43
44
45
46
47
48
# File 'lib/bwapi/error.rb', line 39

def verify_object_class object
  case object
  when Array
    split_array_errors(object)
  when Hash, Hashie::Mash
    split_hash_errors(object)
  when String
    @error_messages << object
  end
end