class Hyperkit::Error

Custom error class for rescuing from all LXD errors

Public Class Methods

error_for_500(response) click to toggle source
# File lib/hyperkit/error.rb, line 108
def self.error_for_500(response)

  if response.body =~ /open: no such file or directory/i
    Hyperkit::NotFound
  elsif response.body =~ /open: is a directory/i
    Hyperkit::BadRequest
  else
    Hyperkit::InternalServerError
  end

end
from_async_operation(response) click to toggle source
# File lib/hyperkit/error.rb, line 47
def self.from_async_operation(response)

  return nil if response.nil? || response[:body].empty?

  begin
    body = JSON.parse(response[:body])
  rescue
    return nil
  end

  if body.has_key?("metadata") && body["metadata"].is_a?(Hash)
    status = body["metadata"]["status_code"].to_i
    from_status(response, status)
  end

end
from_response(response) click to toggle source

Returns the appropriate Hyperkit::Error subclass based on status and response message

@param [Hash] response HTTP response @return [Hyperkit::Error]

# File lib/hyperkit/error.rb, line 33
def self.from_response(response)

  status  = response[:status].to_i

  err = from_status(response, status)

  if err.nil?
    err = from_async_operation(response)
  end

  err

end
from_status(response, status) click to toggle source
# File lib/hyperkit/error.rb, line 64
def self.from_status(response, status)
  if klass = case status
    when 400      then Hyperkit::BadRequest
    when 401      then Hyperkit::Unauthorized
    when 403      then Hyperkit::Forbidden
    when 404      then Hyperkit::NotFound
    when 405      then Hyperkit::MethodNotAllowed
    when 406      then Hyperkit::NotAcceptable
    when 409      then Hyperkit::Conflict
    when 415      then Hyperkit::UnsupportedMediaType
    when 422      then Hyperkit::UnprocessableEntity
    when 400..499 then Hyperkit::ClientError
    when 500      then error_for_500(response)
    when 501      then Hyperkit::NotImplemented
    when 502      then Hyperkit::BadGateway
    when 503      then Hyperkit::ServiceUnavailable
    when 500..599 then Hyperkit::ServerError
    end
    klass.new(response)
  end
end
new(response=nil) click to toggle source
Calls superclass method
# File lib/hyperkit/error.rb, line 86
def initialize(response=nil)
  @response = response
  super(build_error_message)
end

Public Instance Methods

documentation_url() click to toggle source

Documentation URL returned by the API for some errors

@return [String]

# File lib/hyperkit/error.rb, line 94
def documentation_url
  data[:documentation_url] if data.is_a? Hash
end
errors() click to toggle source

Array of validation errors @return [Array<Hash>] Error info

# File lib/hyperkit/error.rb, line 100
def errors
  if data && data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end

Private Instance Methods

build_error_message() click to toggle source
# File lib/hyperkit/error.rb, line 170
def build_error_message
  return nil if @response.nil?

  message = ""

  if ! data.is_a?(Hash) || ! data[:metadata] || ! data[:metadata][:err]
    message = "#{@response[:method].to_s.upcase} "
    message << redact_url(@response[:url].to_s) + ": "
  end

  if data.is_a?(Hash) && data[:metadata] && data[:metadata][:status_code]
    message << "#{data[:metadata][:status_code]} - "
  else
    message << "#{@response[:status]} - "
  end

  message << "#{response_message}" unless response_message.nil?
  message << "#{response_error}" unless response_error.nil?
  message << "#{response_error_summary}" unless response_error_summary.nil?
  message << " // See: #{documentation_url}" unless documentation_url.nil?
  message
end
data() click to toggle source
# File lib/hyperkit/error.rb, line 122
def data
  @data ||=
    if (body = @response[:body]) && !body.empty?
      if body.is_a?(String) &&
        @response[:response_headers] &&
        @response[:response_headers][:content_type] =~ /json/

        Sawyer::Agent.serializer.decode(body)
      else
        body
      end
    else
      nil
    end
end
redact_url(url_string) click to toggle source
# File lib/hyperkit/error.rb, line 193
def redact_url(url_string)
  %w[secret].each do |token|
    url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token
  end
  url_string
end
response_error() click to toggle source
# File lib/hyperkit/error.rb, line 147
def response_error
  err = nil

  if data.is_a?(Hash) && data[:error]
    err = data[:error]
  elsif data.is_a?(Hash) && data[:metadata]
    err = data[:metadata][:err]
  end

  "Error: #{err}" if err
end
response_error_summary() click to toggle source
# File lib/hyperkit/error.rb, line 159
def response_error_summary
  return nil unless data.is_a?(Hash) && !Array(data[:errors]).empty?

  summary = "\nError summary:\n"
  summary << data[:errors].map do |hash|
    hash.map { |k,v| "  #{k}: #{v}" }
  end.join("\n")

  summary
end
response_message() click to toggle source
# File lib/hyperkit/error.rb, line 138
def response_message
  case data
  when Hash
    data[:message]
  when String
    data
  end
end