Exception: Hyperkit::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/hyperkit/error.rb

Overview

Custom error class for rescuing from all LXD errors

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error


86
87
88
89
# File 'lib/hyperkit/error.rb', line 86

def initialize(response=nil)
  @response = response
  super(build_error_message)
end

Class Method Details

.error_for_500(response) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Hyperkit::Error

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


33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

Instance Method Details

#documentation_urlString

Documentation URL returned by the API for some errors


94
95
96
# File 'lib/hyperkit/error.rb', line 94

def documentation_url
  data[:documentation_url] if data.is_a? Hash
end

#errorsArray<Hash>

Array of validation errors


100
101
102
103
104
105
106
# File 'lib/hyperkit/error.rb', line 100

def errors
  if data && data.is_a?(Hash)
    data[:errors] || []
  else
    []
  end
end