Skip to content

Commit

Permalink
Add better error/debug message when none explicitly set for API reque…
Browse files Browse the repository at this point in the history
…st (refs #76)
  • Loading branch information
JonathanTron committed Aug 19, 2015
1 parent 99fd180 commit 3b2fe5b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This file is used to list changes made in each version of grafana.

## dev:

* Ensure we're displaying better messages in some edge cases with HTTP requests [#76](https://github.com/JonathanTron/chef-grafana/issues/76)

## 2.1.0 (2015-08-17):

* Update Grafana default version to 2.1.1
Expand Down
29 changes: 25 additions & 4 deletions libraries/api_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def login(host, port, user, password)
end

handle_response(
request,
response,
success: 'Login was successful.',
unauthorized: 'Invalid username/password.',
Expand Down Expand Up @@ -50,14 +51,26 @@ def with_limited_retry(opts, &block)
end
end

# Format a request for use in error/debug message
# Params:
# +request+:: Net::HTTPRequest
def request_message(request)
"#{request.method} #{request.path}"
end

# Log the right thing when
# Params:
# +request+:: Net::HTTPRequest
# +response+:: Net::HTTPResponse -
# +messages+:: String or -
def handle_response(response, messages)
def handle_response(request, response, messages)
case response
when Net::HTTPSuccess
Chef::Log.debug messages[:success]
if messages[:success].nil?
Chef::Log.debug "Request succeeded when sending #{request_message request}"
else
Chef::Log.debug messages[:success]
end
when Net::HTTPUnauthorized
if messages[:unauthorized].nil?
Chef::Log.error 'Invalid grafana_user and grafana_sess.'
Expand All @@ -66,12 +79,20 @@ def handle_response(response, messages)
end
raise BackendError
when Net::HTTPNotFound
Chef::Log.error messages[:not_found]
if messages[:not_found].nil?
Chef::Log.error "Endpoint not found when sending #{request_message request}"
else
Chef::Log.error messages[:not_found]
end
when nil
Chef::Log.error 'Connection refused.'
raise BackendError
else
Chef::Log.error(messages[:unknown_code] % { code: response.code })
if messages[:unknown_code].nil?
Chef::Log.error "Response code '#{response.code}' not handled when sending #{request_message request}"
else
Chef::Log.error(messages[:unknown_code] % { code: response.code })
end
raise BackendError
end
end
Expand Down
3 changes: 3 additions & 0 deletions libraries/dashboard_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def get_dashboard(dashboard_options, grafana_options)
end

handle_response(
request,
response,
success: 'The dashboard has been successfully retrieved.',
not_found: 'The dashboard does not exist.',
Expand Down Expand Up @@ -48,6 +49,7 @@ def create_dashboard(dashboard_options, grafana_options)
end

handle_response(
request,
response,
success: "Dashboard (#{dashboard_options[:name]}) creation was successful.",
unknown_code: 'DashboardApi::create_dashboard unchecked response code: %{code}'
Expand All @@ -69,6 +71,7 @@ def delete_dashboard(name, grafana_options)
end

handle_response(
request,
response,
success: 'Dashboard deletion was successful.',
unknown_code: 'DashboardApi::delete_dashboard unchecked response code: %{code}'
Expand Down
4 changes: 4 additions & 0 deletions libraries/datasource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def add_data_source(db_options, legacy_http_semantic, grafana_options)
end

handle_response(
request,
response,
success: 'Datasource addition was successful.',
unknown_code: 'DataSourceAPI::add_data_source unchecked response code: %{code}'
Expand Down Expand Up @@ -59,6 +60,7 @@ def update_data_source(db_options, legacy_http_semantic, grafana_options)
end

handle_response(
request,
response,
success: 'Datasource update was successful.',
unknown_code: 'DataSourceAPI::update_data_source unchecked response code: %{code}'
Expand All @@ -83,6 +85,7 @@ def delete_data_source(db_id, grafana_options)
end

handle_response(
request,
response,
success: 'Datasource deletion was successful.',
unknown_code: 'DataSourceAPI::delete_data_source unchecked response code: %{code}'
Expand All @@ -106,6 +109,7 @@ def get_data_source_list(grafana_options)
end

handle_response(
request,
response,
success: 'List of datasources have been successfully retrieved.',
unknown_code: 'Error retrieving list of datasources.'
Expand Down
3 changes: 3 additions & 0 deletions libraries/organization_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def add_org(name, grafana_options)
end

handle_response(
request,
response,
success: 'Organization addition was successful.',
unknown_code: 'OrganizationApi::add_org unchecked response code: %{code}'
Expand All @@ -40,6 +41,7 @@ def update_org(org_options, grafana_options)
end

handle_response(
request,
response,
success: 'Organization update was successful.',
unknown_code: 'OrganizationApi::update_org unchecked response code: %{code}'
Expand All @@ -63,6 +65,7 @@ def get_orgs_list(grafana_options)
end

handle_response(
request,
response,
success: 'The list of organizations has been successfully retrieved.',
unknown_code: 'OrganizationApi::get_orgs_list unchecked response code: %{code}'
Expand Down
2 changes: 2 additions & 0 deletions libraries/user_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def add_global_user(person, grafana_options)
end

handle_response(
request,
response,
success: 'The admin user has been successfully created.',
unknown_code: 'UserApi::add_admin_user unchecked response code: %{code}'
Expand All @@ -40,6 +41,7 @@ def get_global_user_list(grafana_options)
end

handle_response(
request,
response,
success: 'The list of users has been successfully retrieved.',
unknown_code: 'UserApi::get_admin_user_list unchecked response code: %{code}'
Expand Down

0 comments on commit 3b2fe5b

Please sign in to comment.