class ChefCLI::ServiceExceptionInspectors::HTTP

def attempt_error_message_extract

def attempt_error_message_extract
  error_body = FFI_Yajl::Parser.parse(response_body)
  if error_body.respond_to?(:key?) && error_body.key?("error")
    Array(error_body["error"]).join(", ")
  else
    error_body.to_s
  end
rescue
  response_body
end

def code

def code
  response.code
end

def extended_error_info

def extended_error_info
  <<~END
    --- REQUEST DATA ----
    #{http_method} #{uri}
    #{request_headers}
    #{req_body}
    --- RESPONSE DATA ---
    #{code} #{response_message}
    #{response_headers}
    #{response_body}
  END
end

def http_method

def http_method
  request.method
end

def initialize(exception)

def initialize(exception)
  @exception = exception
end

def message

def message
  "HTTP #{code} #{response_message}: " + parsed_response_body
end

def parsed_response_body

def parsed_response_body
  if response_body && !response_body.empty?
    attempt_error_message_extract
  else
    "(No explanation provided by server)"
  end
end

def req_body

def req_body
  request.body
end

def request

def request
  exception.chef_rest_request
end

def request_headers

def request_headers
  headers_s = ""
  request.each_header do |key, value|
    headers_s << key << ": " << value << "\n"
  end
  headers_s
end

def response

def response
  exception.response
end

def response_body

def response_body
  response.body
end

def response_headers

def response_headers
  headers_s = ""
  response.each_header do |key, value|
    headers_s << key << ": " << value << "\n"
  end
  headers_s
end

def response_message

def response_message
  response.message
end

def uri

def uri
  request.uri.to_s + request.path.to_s
end