Exception: Updox::UpdoxException

Inherits:
Exception
  • Object
show all
Defined in:
lib/updox/updox_exception.rb

Class Method Summary collapse

Class Method Details

.from_response(response, msg: nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/updox/updox_exception.rb', line 3

def self.from_response(response, msg: nil)
  data = {}
  exception_msg = "Failed #{msg}:"
  data['HTTP_CODE'] = response.code

  begin
    error_response = JSON.parse(response.body)

    if error_response.is_a?(Hash)
      if error_response.include?('responseMessage')
        data['MSG'] = error_response['responseMessage']
      end

      if error_response.include?('responseCode')
        data['UPDOX_CODE'] = error_response['responseCode']
      end
    else
      data['MSG'] = error_response
    end
  rescue JSON::ParserError
    data['MSG'] = response.body
  end

  data.each {|k,v| exception_msg << " #{k} '#{v}'" }

  return UpdoxException.new(exception_msg)
end