Skip to content

Commit

Permalink
Merge branch 'OC-1880-error-reporting'
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeleo committed Jul 5, 2012
2 parents 762eaca + e6268e7 commit cc22160
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 84 deletions.
95 changes: 39 additions & 56 deletions chef/lib/chef/formatters/base.rb
@@ -1,5 +1,26 @@
#
# Author:: Tyler Cloke (<tyler@opscode.com>)
#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/event_dispatch/base'
require 'chef/formatters/error_inspectors'
require 'chef/formatters/error_descriptor'
require 'chef/formatters/error_mapper'

class Chef

Expand Down Expand Up @@ -72,45 +93,13 @@ def puts(string, *colors)

end

class ErrorDescription

attr_reader :sections

def initialize(title)
@title = title
@sections = []
end

def section(heading, text)
@sections << [heading, text]
end

def display(out)
out.puts "=" * 80
out.puts @title, :red
out.puts "=" * 80
out.puts "\n"
sections.each do |section|
display_section(section, out)
end
end

private

def display_section(section, out)
heading, text = section
out.puts heading
out.puts "-" * heading.size
out.puts text
out.puts "\n"
end

end

# == Formatters::Base
# Base class that all formatters should inherit from.
class Base < EventDispatch::Base

include ErrorMapper

def self.cli_name(name)
Chef::Formatters.register(name, self)
end
Expand All @@ -131,48 +120,42 @@ def print(*args)
@output.print(*args)
end

def describe_error(headline, error_inspector)
description = ErrorDescription.new(headline)
error_inspector.add_explanation(description)
# Input: a Formatters::ErrorDescription object.
# Outputs error to SDOUT.
def display_error(description)
puts("")
description.display(output)
end

# Failed to register this client with the server.
def registration_failed(node_name, exception, config)
error_inspector = ErrorInspectors::RegistrationErrorInspector.new(node_name, exception, config)
headline = "Chef encountered an error attempting to create the client \"#{node_name}\""
describe_error(headline, error_inspector)
#A Formatters::ErrorDescription object
description = ErrorMapper.registration_failed_helper(node_name, exception, config)
display_error(description)
end

def node_load_failed(node_name, exception, config)
error_inspector = ErrorInspectors::NodeLoadErrorInspector.new(node_name, exception, config)
headline = "Chef encountered an error attempting to load the node data for \"#{node_name}\""
describe_error(headline, error_inspector)
description = ErrorMapper.node_load_failed_helper(node_name, exception, config)
display_error(description)
end

def run_list_expand_failed(node, exception)
error_inspector = ErrorInspectors::RunListExpansionErrorInspector.new(node, exception)
headline = "Error expanding the run_list:"
describe_error(headline, error_inspector)
description = ErrorMapper.run_list_expand_failed_helper(node, exception)
display_error(description)
end

def cookbook_resolution_failed(expanded_run_list, exception)
error_inspector = ErrorInspectors::CookbookResolveErrorInspector.new(expanded_run_list, exception)
headline = "Error Resolving Cookbooks for Run List:"
describe_error(headline, error_inspector)
description = ErrorMapper.cookbook_resolution_failed_helper(expanded_run_list, exception)
display_error(description)
end

def cookbook_sync_failed(cookbooks, exception)
error_inspector = ErrorInspectors::CookbookSyncErrorInspector.new(cookbooks, exception)
headline = "Error Syncing Cookbooks:"
describe_error(headline, error_inspector)
description = ErrorMapper.cookbook_sync_failed_helper(cookbooks, exception)
display_error(description)
end

def resource_failed(resource, action, exception)
error_inspector = ErrorInspectors::ResourceFailureInspector.new(resource, action, exception)
headline = "Error executing action `#{action}` on resource '#{resource}'"
describe_error(headline, error_inspector)
description = ErrorMapper.resource_failed_helper(resource, action, exception)
display_error(description)
end

# Generic callback for any attribute/library/lwrp/recipe file in a
Expand Down
66 changes: 66 additions & 0 deletions chef/lib/chef/formatters/error_descriptor.rb
@@ -0,0 +1,66 @@
#
# Author:: Tyler Cloke (<tyler@opscode.com>)
#
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
module Formatters
# == Formatters::ErrorDescription
# Class for displaying errors on STDOUT.
class ErrorDescription

attr_reader :sections

def initialize(title)
@title = title
@sections = []
end

def section(heading, text)
@sections << [heading, text]
end

def display(out)
out.puts "=" * 80
out.puts @title, :red
out.puts "=" * 80
out.puts "\n"
sections.each do |section|
display_section(section, out)
end
end

def for_json()
{
'title' => @title,
'sections' => @sections
}
end

private

def display_section(section, out)
heading, text = section
out.puts heading
out.puts "-" * heading.size
out.puts text
out.puts "\n"
end

end
end
end
@@ -1,5 +1,6 @@
#--
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: Tyler Cloke (<tyler@opscode.com>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
Expand Down Expand Up @@ -84,7 +85,7 @@ def dynamic_resource?
end

def filtered_bt
exception.backtrace.select {|l| l =~ /^#{Chef::Config.file_cache_path}/ }
Array( exception.backtrace ).select {|l| l =~ /^#{Chef::Config.file_cache_path}/ }
end

private
Expand Down
@@ -1,14 +1,15 @@
#--
# Author:: Daniel DeLeo (<dan@opscode.com>)
# Author:: Tyler Cloke (<tyler@opscode.com>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
78 changes: 78 additions & 0 deletions chef/lib/chef/formatters/error_mapper.rb
@@ -0,0 +1,78 @@
#--
# Author:: Tyler Cloke (<tyler@opscode.com>)
# Copyright:: Copyright (c) 2012 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

class Chef
module Formatters
# == Formatters::ErrorMapper
# Collection of methods for creating and returning
# Formatters::ErrorDescription objects based on node,
# exception, and configuration information.
module ErrorMapper

# Failed to register this client with the server.
def self.registration_failed_helper(node_name, exception, config)
error_inspector = ErrorInspectors::RegistrationErrorInspector.new(node_name, exception, config)
headline = "Chef encountered an error attempting to create the client \"#{node_name}\""
description = ErrorDescription.new(headline)
error_inspector.add_explanation(description)
return description
end

def self.node_load_failed_helper(node_name, exception, config)
error_inspector = ErrorInspectors::NodeLoadErrorInspector.new(node_name, exception, config)
headline = "Chef encountered an error attempting to load the node data for \"#{node_name}\""
description = ErrorDescription.new(headline)
error_inspector.add_explanation(description)
return description
end

def self.run_list_expand_failed_helper(node, exception)
error_inspector = ErrorInspectors::RunListExpansionErrorInspector.new(node, exception)
headline = "Error expanding the run_list:"
description = ErrorDescription.new(headline)
error_inspector.add_explanation(description)
return description
end

def self.cookbook_resolution_failed_helper(expanded_run_list, exception)
error_inspector = ErrorInspectors::CookbookResolveErrorInspector.new(expanded_run_list, exception)
headline = "Error Resolving Cookbooks for Run List:"
description = ErrorDescription.new(headline)
error_inspector.add_explanation(description)
return description
end

def self.cookbook_sync_failed_helper(cookbooks, exception)
error_inspector = ErrorInspectors::CookbookSyncErrorInspector.new(cookbooks, exception)
headline = "Error Syncing Cookbooks:"
description = ErrorDescription.new(headline)
error_inspector.add_explanation(description)
return description
end

def self.resource_failed_helper(resource, action, exception)
error_inspector = ErrorInspectors::ResourceFailureInspector.new(resource, action, exception)
headline = "Error executing action `#{action}` on resource '#{resource}'"
description = ErrorDescription.new(headline)
error_inspector.add_explanation(description)
return description
end

end
end
end

0 comments on commit cc22160

Please sign in to comment.