Exception: SimpleSpark::Exceptions::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/simple_spark/exceptions.rb

Overview

Adding an object to the exception to pass errors back begin

raise Error.new({ id: '1' }), "a message"

rescue Error => e

puts e.message # => "a message"
puts e.object # => { id: '1' }

end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil, results = {}) ⇒ Error

Returns a new instance of Error.



13
14
15
16
# File 'lib/simple_spark/exceptions.rb', line 13

def initialize(object = nil, results = {})
  @object = object
  @results = results
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



11
12
13
# File 'lib/simple_spark/exceptions.rb', line 11

def object
  @object
end

#resultsObject (readonly)

Returns the value of attribute results.



11
12
13
# File 'lib/simple_spark/exceptions.rb', line 11

def results
  @results
end

Class Method Details

.fail_with_exception_for_status(status, errors, results) ⇒ Object



22
23
24
25
# File 'lib/simple_spark/exceptions.rb', line 22

def self.fail_with_exception_for_status(status, errors, results)
  exception = status_codes[status.to_s] || status_codes['default']
  fail exception.new(errors, results), errors.map { |e| "#{e['message']} #{status} (Error Code: #{e['code']})" + (e['description'] ? ": #{e['description']}" : '') }.join(', ')
end

.status_codesObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/simple_spark/exceptions.rb', line 27

def self.status_codes
  {
    'default' => Exceptions::UnprocessableEntity,
    '400' => Exceptions::BadRequest,
    '404' => Exceptions::NotFound,
    '422' => Exceptions::UnprocessableEntity,
    '420' => Exceptions::ThrottleLimitExceeded,
    '429' => Exceptions::ThrottleLimitExceeded
  }
end

Instance Method Details

#transmission_idObject



18
19
20
# File 'lib/simple_spark/exceptions.rb', line 18

def transmission_id
  results['id']
end