Exception: LayerVault::Response::Error

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

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = nil) ⇒ Error

Returns a new instance of Error.



27
28
29
30
# File 'lib/layervault/response/error.rb', line 27

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

Class Method Details

.from_response(response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/layervault/response/error.rb', line 5

def self.from_response(response)
  status  = response[:status].to_i
  body    = response[:body].to_s
  headers = response[:response_headers]

  if klass =  case status
                when 400      then LayerVault::Response::BadRequest
                when 401      then LayerVault::Response::Unauthorized
                when 403      then LayerVault::Response::Forbidden
                when 404      then LayerVault::Response::NotFound
                when 406      then LayerVault::Response::NotAcceptable
                when 400..499 then LayerVault::Response::ClientError
                when 500      then LayerVault::Response::InternalServerError
                when 501      then LayerVault::Response::NotImplemented
                when 502      then LayerVault::Response::BadGateway
                when 503      then LayerVault::Response::ServiceUnavailable
                when 500..599 then LayerVault::Response::ServerError
              end
    klass.new(response)
  end
end

Instance Method Details

#errorsObject



32
33
34
35
36
37
38
# File 'lib/layervault/response/error.rb', line 32

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