Exception: Luma::LumaException

Inherits:
Exception
  • Object
show all
Defined in:
lib/luma/luma_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
# File 'lib/luma/luma_exception.rb', line 3

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

  begin
    error_response = JSON.parse(response.body)

    if (error_response.is_a?(Hash) && error_response.include?("Meta") && error_response["Meta"].include?("Errors"))
      exception_msg << error_response["Meta"]["Errors"]
    elsif (error_response.is_a?(Hash) && error_response.include?("message") && error_response.include?("detail"))
      exception_msg << error_response["message"] + ": " + error_response["detail"]
    end
  rescue JSON::ParserError
    exception_msg << response.body
  end

  return LumaException.new(exception_msg)
end