Class: CloudFoundry::Client::Response::CloudError

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/cloudfoundry/client/response.rb

Overview

Faraday Response Middleware to parse cloud errors.

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object

Checks if an error is returned by target cloud.

Parameters:

  • env (Hash)

    Faraday environment.

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cloudfoundry/client/response.rb', line 32

def on_complete(env)
  if env[:status].to_i >= 400
    err = case env[:status].to_i
      when 400 then CloudFoundry::Client::Exception::BadRequest
      when 403 then CloudFoundry::Client::Exception::Forbidden
      when 404 then CloudFoundry::Client::Exception::NotFound
      when 500 then CloudFoundry::Client::Exception::ServerError
      when 502 then CloudFoundry::Client::Exception::BadGateway
    end
    raise err, parse_cloud_error_message(env[:status], env[:body])
  end
end

#parse_cloud_error_message(status, body) ⇒ String

Parses a CloudFoundry error message.

Parameters:

  • status (String)

    Faraday HTTP response status.

  • body (String)

    Faraday HTTP response body.

Returns:

  • (String)

    CloudFoundry error message.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cloudfoundry/client/response.rb', line 50

def parse_cloud_error_message(status, body)
  parsed_body = JSON.parse(body, :symbolize_names => true)
  if parsed_body && parsed_body[:code] && parsed_body[:description]
    description = parsed_body[:description].gsub("\"","'")
    "Error #{parsed_body[:code]}: #{description}"
  else
    "Error (HTTP #{status}): #{body}"
  end
rescue JSON::ParserError
  if body.nil? || body.empty?
    "Error (#{status}): No Response Received"
  else
    "Error (JSON #{status}): #{body}"
  end
end