Exception: Paychex::Error

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

Overview

Custom error class for rescuing from all paychex errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Error

Returns a new instance of Error.



6
7
8
9
10
11
12
# File 'lib/paychex/error.rb', line 6

def initialize(response)
  super
  @response = response.dup
  @http_method = response.method.to_s
  @url = response.url
  @raw_errors = response.body.fetch('errors') if response.body.is_a?(Hash) && !response.body.empty? && !response.body.fetch('errors', nil).nil?
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/paychex/error.rb', line 4

def errors
  @errors
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



4
5
6
# File 'lib/paychex/error.rb', line 4

def http_method
  @http_method
end

#raw_errorsObject (readonly)

Returns the value of attribute raw_errors.



4
5
6
# File 'lib/paychex/error.rb', line 4

def raw_errors
  @raw_errors
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/paychex/error.rb', line 4

def response
  @response
end

#urlObject (readonly)

Returns the value of attribute url.



4
5
6
# File 'lib/paychex/error.rb', line 4

def url
  @url
end

Instance Method Details

#error_sentenceObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/paychex/error.rb', line 23

def error_sentence
  return if @raw_errors.nil?

  array = []
  @raw_errors.each do |v|
    array += "#{v['code']}: #{v['description']}, #{v['resolution']}"
  end

  array.join(' ')
end

#messageObject



14
15
16
17
18
19
20
21
# File 'lib/paychex/error.rb', line 14

def message
  <<-HEREDOC
    URL: #{@response.url}
    method: #{@response.method}
    response status: #{@response.status}
    response body: #{@response.response.body}
  HEREDOC
end