Module: Chef::Formatters::APIErrorFormatting

Constant Summary collapse

NETWORK_ERROR_CLASSES =
[Errno::ECONNREFUSED, Timeout::Error, Errno::ETIMEDOUT, SocketError]

Instance Method Summary collapse

Instance Method Details

#api_keyObject



98
99
100
# File 'lib/chef/formatters/error_inspectors/api_error_formatting.rb', line 98

def api_key
  config[:client_key]
end

#clock_skew?Boolean



106
107
108
# 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) ⇒ Object



61
62
63
64
65
66
# 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).\n")
  error_description.section("Server Response:",format_rest_error)
end

#describe_401_error(error_description) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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).\nThe request failed because your clock has drifted by more than 15 minutes.\nSyncing your clock to an NTP Time source should resolve the issue.\n")
  else
    error_description.section("Authentication Error:","Failed to authenticate to the chef server (http 401).\n")

    error_description.section("Server Response:", format_rest_error)
    error_description.section("Relevant Config Settings:","chef_server_url   \"\#{server_url}\"\nnode_name         \"\#{username}\"\nclient_key        \"\#{api_key}\"\n\nIf these settings are correct, your client_key may be invalid, or\nyou may have a chef user with the same client name as this node.\n")
  end
end

#describe_500_error(error_description) ⇒ Object



68
69
70
71
72
73
# 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.\n")
  error_description.section("Server Response:", format_rest_error)
end

#describe_503_error(error_description) ⇒ Object



75
76
77
78
# 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) ⇒ Object

Fallback for unexpected/uncommon http errors



82
83
84
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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}\n\nYour chef_server_url may be misconfigured, or the network could be down.\n")
  error_description.section("Relevant Config Settings:","chef_server_url  \"\#{server_url}\"\n")
end

#format_rest_errorObject

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



88
89
90
91
92
# 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_errorObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# 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_urlObject



102
103
104
# File 'lib/chef/formatters/error_inspectors/api_error_formatting.rb', line 102

def server_url
  config[:chef_server_url]
end

#usernameObject



94
95
96
# File 'lib/chef/formatters/error_inspectors/api_error_formatting.rb', line 94

def username
  config[:node_name]
end