class Gapic::Rest::Error

Gapic REST exception class

def initialize message, status_code, status: nil, details: nil, headers: nil

Parameters:
  • headers (Object, nil) -- Http headers data of this error
  • details (Object, nil) -- Details data of this error
  • status (String, nil) -- The text representation of status as parsed from the response body
  • status_code (Integer, nil) -- HTTP status code of this error
  • message (String, nil) -- error message
def initialize message, status_code, status: nil, details: nil, headers: nil
  super message
  @status_code = status_code
  @status = status
  @details = details
  @headers = headers
end

def parse_details details

Returns:
  • (Object, nil) -

Parameters:
  • details (Object, nil) -- the details object

Other tags:
    Private: -
def parse_details details
  # For rest errors details will contain json representations of `Protobuf.Any`
  # decoded into hashes. If it's not an array, of its elements are not hashes,
  # it's some other case
  return details unless details.is_a? ::Array
  details.map do |detail_instance|
    next detail_instance unless detail_instance.is_a? ::Hash
    # Next, parse detail_instance into a Proto message.
    # There are three possible issues for the JSON->Any->message parsing
    # - json decoding fails
    # - the json belongs to a proto message type we don't know about
    # - any unpacking fails
    # If we hit any of these three issues we'll just return the original hash
    begin
      any = ::Google::Protobuf::Any.decode_json detail_instance.to_json
      klass = ::Google::Protobuf::DescriptorPool.generated_pool.lookup(any.type_name)&.msgclass
      next detail_instance if klass.nil?
      unpack = any.unpack klass
      next detail_instance if unpack.nil?
      unpack
    rescue ::Google::Protobuf::ParseError
      detail_instance
    end
  end.compact
end

def parse_faraday_error err

Returns:
  • (Array(String, String, String, String, String)) -

Parameters:
  • err (Faraday::Error) -- the Faraday error to extract information from

Other tags:
    Private: -
def parse_faraday_error err
  message = err.message
  status_code = err.response_status
  status = nil
  details = nil
  headers = err.response_headers
  if err.response_body
    msg, code, status, details = try_parse_from_body err.response_body
    message = "An error has occurred when making a REST request: #{msg}" unless msg.nil?
    status_code = code unless code.nil?
  end
  [message, status_code, status, details, headers]
end

def try_parse_from_body body_str

Returns:
  • (Array(String, String, String, String)) -

Parameters:
  • body_str (String) --

Other tags:
    Private: -
def try_parse_from_body body_str
  body = JSON.parse body_str
  unless body.is_a?(::Hash) && body&.key?("error") && body["error"].is_a?(::Hash)
    return [nil, nil, nil, nil]
  end
  error = body["error"]
  message = error["message"] if error.key? "message"
  code = error["code"] if error.key? "code"
  status = error["status"] if error.key? "status"
  details = parse_details error["details"] if error.key? "details"
  [message, code, status, details]
rescue JSON::ParserError
  [nil, nil, nil, nil]
end

def wrap_faraday_error err

Returns:
  • (Gapic::Rest::Error) -

Parameters:
  • err (Faraday::Error) -- the Faraday error to wrap
def wrap_faraday_error err
  message, status_code, status, details, headers = parse_faraday_error err
  Gapic::Rest::Error.new message, status_code, status: status, details: details, headers: headers
end