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
# File 'lib/updox/updox_exception.rb', line 3

def self.from_response(response, msg: nil)
  exception_msg = "Failed #{msg}:"
  exception_msg << " HTTP code: #{response.code}"

  begin
    error_response = JSON.parse(response.body)

    if error_response.is_a?(Hash)
      if error_response.include?('responseMessage')
        exception_msg << " MSG: #{error_response['responseMessage']}"
      end

      if error_response.include?('responseCode')
        exception_msg << " UPDOX CODE: #{error_response['responseCode']}"
      end
    else
      exception_msg << " MSG: #{error_response}"
    end
  rescue JSON::ParserError
    exception_msg << " MSG: #{response.body}"
  end

  return UpdoxException.new(exception_msg)
end