module Chef::Formatters::APIErrorFormatting

Constants

NETWORK_ERROR_CLASSES

Public Instance Methods

api_key() click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 98
def api_key
  config[:client_key]
end
clock_skew?() click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 106
def clock_skew?
  exception.response.body =~ /synchronize the clock/i
end
describe_400_error(error_description) click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 61
      def describe_400_error(error_description)
        error_description.section("Invalid Request Data:","The data in your request was invalid (HTTP 400).
")
        error_description.section("Server Response:",format_rest_error)
      end
describe_401_error(error_description) click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 37
      def describe_401_error(error_description)
        if clock_skew?
          error_description.section("Authentication Error:","Failed to authenticate to the chef server (http 401).
The request failed because your clock has drifted by more than 15 minutes.
Syncing your clock to an NTP Time source should resolve the issue.
")
        else
          error_description.section("Authentication Error:","Failed to authenticate to the chef server (http 401).
")

          error_description.section("Server Response:", format_rest_error)
          error_description.section("Relevant Config Settings:","chef_server_url   "#{server_url}"
node_name         "#{username}"
client_key        "#{api_key}"

If these settings are correct, your client_key may be invalid, or
you may have a chef user with the same client name as this node.
")
        end
      end
describe_500_error(error_description) click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 68
      def describe_500_error(error_description)
        error_description.section("Unknown Server Error:","The server had a fatal error attempting to load the node data.
")
        error_description.section("Server Response:", format_rest_error)
      end
describe_503_error(error_description) click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 75
def describe_503_error(error_description)
  error_description.section("Server Unavailable","The Chef Server is temporarily unavailable")
  error_description.section("Server Response:", format_rest_error)
end
describe_http_error(error_description) click to toggle source

Fallback for unexpected/uncommon http errors

# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 82
def describe_http_error(error_description)
  error_description.section("Unexpected API Request Failure:", format_rest_error)
end
describe_network_errors(error_description) click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 26
      def describe_network_errors(error_description)
        error_description.section("Networking Error:","#{exception.message}

Your chef_server_url may be misconfigured, or the network could be down.
")
        error_description.section("Relevant Config Settings:","chef_server_url  "#{server_url}"
")
      end
format_rest_error() click to toggle source

Parses JSON from the error response sent by Chef Server and returns the error message

# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 88
def format_rest_error
  Array(Chef::JSONCompat.from_json(exception.response.body)["error"]).join('; ')
rescue Exception
  safe_format_rest_error
end
safe_format_rest_error() click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 110
def safe_format_rest_error
  # When we get 504 from the server, sometimes the response body is non-readable.
  #
  # Stack trace:
  #
  # NoMethodError: undefined method `closed?' for nil:NilClass
  # .../lib/ruby/1.9.1/net/http.rb:2789:in `stream_check'
  # .../lib/ruby/1.9.1/net/http.rb:2709:in `read_body'
  # .../lib/ruby/1.9.1/net/http.rb:2736:in `body'
  # .../lib/chef/formatters/error_inspectors/api_error_formatting.rb:91:in `rescue in format_rest_error'
  begin
    exception.response.body
  rescue Exception
    "Cannot fetch the contents of the response."
  end
end
server_url() click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 102
def server_url
  config[:chef_server_url]
end
username() click to toggle source
# File lib/chef/formatters/error_inspectors/api_error_formatting.rb, line 94
def username
  config[:node_name]
end