Class: Deltacloud::ErrorResponse

Inherits:
Faraday::Response::Middleware
  • Object
show all
Includes:
Client::Helpers::Model
Defined in:
lib/deltacloud/error_response.rb

Instance Method Summary collapse

Methods included from Client::Helpers::Model

#error, #from_collection, #from_resource, #model

Instance Method Details

#call(env) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/deltacloud/error_response.rb', line 51

def call(env)
  @app.call(env).on_complete do |e|
    case e[:status].to_s
    when '401'
      raise client_error(:authentication_error, e,
        'Invalid :api_user or :api_password')
    when '405'
      raise client_error(
        :invalid_state, e, 'Resource state does not permit this action'
      )
    when '404'
      raise client_error(:not_found, e, 'Object not found')
    when /40\d/
      raise client_error(:client_failure, e)
    when '500'
      raise client_error(:server_error, e)
    when '502'
      raise client_error(:backend_error, e)
    when '501'
      raise client_error(:not_supported, e)
    end
  end
end

#client_error(name, error, message = nil) ⇒ Object

This method tries to parse the error XML from Deltacloud API In case there is no error returned in body, it will try to use the generic error reporting.

  • name -> Deltacloud::Client::Class

  • error -> Deltacloud XML error representation

  • message -> Exception message (overiden by error body message if

    present)
    


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/deltacloud/error_response.rb', line 30

def client_error(name, error, message=nil)
  args = {
    :message => message,
    :status => error ? error[:status] : '500'
  }
  # If Deltacloud API sends an error in the response body, parse it.
  # Otherwise, when DC API sends just plain text error, use
  # it as the exception message.
  # If DC API does not send anything back, then fallback to
  # the 'message' attribute.
  #
  if error and !error[:body].empty?
    if xml_error?(error)
      args.merge! parse_error(error[:body].to_xml.root)
    else
      args[:message] = error[:body]
    end
  end
  error(name).new(args)
end