Exception: Apruve::Error

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

Overview

Custom error class for rescuing from all API response-related Apruve errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.

Parameters:

  • response (Hash) (defaults to: nil)

    the decoded json response body



8
9
10
11
12
13
14
# File 'lib/apruve/error.rb', line 8

def initialize(response=nil)
  @response = response
  unless response.nil?
    super error_message
    # super 'Error!'
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/apruve/error.rb', line 5

def response
  @response
end

Instance Method Details

#bodyHash

Returns:

  • (Hash)


17
18
19
20
21
22
# File 'lib/apruve/error.rb', line 17

def body
  @body ||= begin
    return {} unless response[:body]
    Utils.indifferent_read_access(response[:body])
  end
end

#error_messageObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/apruve/error.rb', line 24

def error_message
  # set_attrs
  errors = body.fetch('errors', nil)
  unless errors.nil?
    error = errors[0]
    extra = error[:source] ? " -- #{error[:source][:parameter]}" : ""
    extra += error[:detail] ? " -- #{error[:detail]}" : ""
    "#{self.class.name}(#{response[:status]}):: "\
    "#{response[:method].to_s.upcase} #{response[:url].to_s}: "\
    "#{error[:title]} #{extra}"
  end
end