Skip to content

Commit f6b5483

Browse files
author
Kevin Paulisse
committed
Move remaining errors
1 parent 2ede61f commit f6b5483

8 files changed

Lines changed: 34 additions & 25 deletions

File tree

lib/octocatalog-diff/catalog-diff/differ.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'stringio'
88

99
require_relative '../catalog'
10+
require_relative '../errors'
1011
require_relative 'filter'
1112

1213
module OctocatalogDiff
@@ -55,10 +56,6 @@ module CatalogDiff
5556
# The heavy lifting is still handled by 'hashdiff' but we're pre-simplifying the input and post-processing
5657
# the output to make it easier to deal with later.
5758
class Differ
58-
# This class is to distinguish handled errors from unhandled ones, for spec testing.
59-
class DifferError < RuntimeError
60-
end
61-
6259
# Constructor
6360
# @param catalog1_in [OctocatalogDiff::Catalog] First catalog to compare
6461
# @param catalog2_in [OctocatalogDiff::Catalog] Second catalog to compare
@@ -594,7 +591,7 @@ def dig_out_key(hash_in, key_array)
594591
# @return [Hash] Internal simplified hash object
595592
def catalog_resources(catalog_in, name = 'Passed catalog')
596593
return catalog_in.resources if catalog_in.is_a?(OctocatalogDiff::Catalog)
597-
raise DifferError, "#{name} is not a valid catalog (input datatype: #{catalog_in.class})"
594+
raise OctocatalogDiff::Errors::DifferError, "#{name} is not a valid catalog (input datatype: #{catalog_in.class})"
598595
end
599596

600597
# Turn array of resources into a hash by serialized keys. For consistency with 'hashdiff'

lib/octocatalog-diff/catalog-util/enc/pe.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ class ENC
1111
# Support the Puppet Enterprise classification API.
1212
# Documentation: https://docs.puppet.com/pe/latest/nc_index.html
1313
class PE
14-
# Error class that can be caught
15-
class ClassificationError < RuntimeError; end
16-
1714
# Allow the main ENC object to retrieve these values
1815
attr_reader :content, :error_message
1916

@@ -70,7 +67,7 @@ def execute(logger)
7067
begin
7168
@content = @api.result(response[:parsed], logger)
7269
@error_message = nil
73-
rescue OctocatalogDiff::CatalogUtil::ENC::PE::ClassificationError => exc
70+
rescue OctocatalogDiff::Errors::PEClassificationError => exc
7471
@error_message = exc.message
7572
logger.error "PE ENC failed: #{exc.message}"
7673
return

lib/octocatalog-diff/catalog-util/enc/pe/v1.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
require 'uri'
55
require 'yaml'
66

7+
require_relative '../../../errors'
8+
79
module OctocatalogDiff
810
module CatalogUtil
911
class ENC
@@ -50,7 +52,7 @@ def result(parsed, logger)
5052
%w(classes parameters).each do |required_key|
5153
next if parsed[required_key]
5254
logger.debug parsed.keys.inspect
53-
raise OctocatalogDiff::CatalogUtil::ENC::PE::ClassificationError, "Response missing: #{required_key}"
55+
raise OctocatalogDiff::Errors::PEClassificationError, "Response missing: #{required_key}"
5456
end
5557

5658
obj = { 'classes' => parsed['classes'], 'parameters' => parsed['parameters'] }

lib/octocatalog-diff/catalog-util/git.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
require 'shellwords'
77
require 'tempfile'
88

9+
require_relative '../errors'
10+
911
module OctocatalogDiff
1012
module CatalogUtil
1113
# Class to perform a git checkout (via 'git archive') of a branch from the base git
1214
# directory into another targeted directory.
1315
class Git
14-
# Trapped errors
15-
class GitCheckoutError < RuntimeError
16-
end
17-
1816
# Check out a branch via 'git archive' from one directory into another.
1917
# @param options [Hash] Options hash:
2018
# - :branch => Branch name to check out
@@ -28,8 +26,12 @@ def self.check_out_git_archive(options = {})
2826
logger = options.fetch(:logger)
2927

3028
# Validate parameters
31-
raise GitCheckoutError, "Source directory #{dir.inspect} does not exist" if dir.nil? || !File.directory?(dir)
32-
raise GitCheckoutError, "Target directory #{path.inspect} does not exist" if dir.nil? || !File.directory?(path)
29+
if dir.nil? || !File.directory?(dir)
30+
raise OctocatalogDiff::Errors::GitCheckoutError, "Source directory #{dir.inspect} does not exist"
31+
end
32+
if dir.nil? || !File.directory?(path)
33+
raise OctocatalogDiff::Errors::GitCheckoutError, "Target directory #{path.inspect} does not exist"
34+
end
3335

3436
# To get the options working correctly (-o pipefail in particular) this needs to run under
3537
# bash. It's just creating a script, rather than figuring out all the shell escapes...
@@ -44,7 +46,9 @@ def self.check_out_git_archive(options = {})
4446

4547
logger.debug("Begin git archive #{dir}:#{branch} -> #{path}")
4648
output, status = Open3.capture2e(tmp_script.path, chdir: dir)
47-
raise GitCheckoutError, "Git archive #{branch}->#{path} failed: #{output}" unless status.exitstatus.zero?
49+
unless status.exitstatus.zero?
50+
raise OctocatalogDiff::Errors::GitCheckoutError, "Git archive #{branch}->#{path} failed: #{output}"
51+
end
4852
logger.debug("Success git archive #{dir}:#{branch}")
4953
ensure
5054
FileUtils.rm_f tmp_script.path if File.exist?(tmp_script.path)
@@ -58,7 +62,9 @@ def self.check_out_git_archive(options = {})
5862
def self.branch_sha(options = {})
5963
branch = options.fetch(:branch)
6064
dir = options.fetch(:basedir)
61-
raise GitCheckoutError, "Git directory #{dir.inspect} does not exist" if dir.nil? || !File.directory?(dir)
65+
if dir.nil? || !File.directory?(dir)
66+
raise OctocatalogDiff::Errors::GitCheckoutError, "Git directory #{dir.inspect} does not exist"
67+
end
6268
repo = Rugged::Repository.new(dir)
6369
repo.branches[branch].target_id
6470
end

lib/octocatalog-diff/cli/printer.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
# frozen_string_literal: true
22

33
require_relative '../catalog-diff/display'
4+
require_relative '../errors'
45

56
module OctocatalogDiff
67
class Cli
78
# Wrapper around OctocatalogDiff::CatalogDiff::Display to set the options and
89
# output to a file or the screen depending on selection.
910
class Printer
10-
# Class for thrown exceptions
11-
class PrinterError < RuntimeError
12-
end
13-
1411
# Constructor
1512
# @param options [Hash] Options from cli/options
1613
# @param logger [Logger] Logger object
@@ -45,7 +42,7 @@ def output_to_file(diff_in)
4542
@logger.info "Wrote diff to #{@options[:output_file]}"
4643
rescue Errno::ENOENT, Errno::EACCES, Errno::EISDIR => exc
4744
@logger.error "Cannot write to #{@options[:output_file]}: #{exc}"
48-
raise PrinterError, "Cannot write to #{@options[:output_file]}: #{exc}"
45+
raise OctocatalogDiff::Errors::PrinterError, "Cannot write to #{@options[:output_file]}: #{exc}"
4946
end
5047
end
5148
end

lib/octocatalog-diff/errors.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class BootstrapError < RuntimeError; end
1212
class CatalogError < RuntimeError; end
1313
class PuppetVersionError < RuntimeError; end
1414
class ReferenceValidationError < RuntimeError; end
15+
class GitCheckoutError < RuntimeError; end
1516

1617
# Error classes for retrieving facts
1718
class FactSourceError < RuntimeError; end
@@ -21,5 +22,12 @@ class FactRetrievalError < RuntimeError; end
2122
class PuppetDBNodeNotFoundError < RuntimeError; end
2223
class PuppetDBGenericError < RuntimeError; end
2324
class PuppetDBConnectionError < RuntimeError; end
25+
26+
# Errors for Puppet Enterprise
27+
class PEClassificationError < RuntimeError; end
28+
29+
# Miscellanous catalog-diff errors
30+
class DifferError < RuntimeError; end
31+
class PrinterError < RuntimeError; end
2432
end
2533
end

spec/octocatalog-diff/tests/catalog-diff/differ_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative '../spec_helper'
44
require OctocatalogDiff::Spec.require_path('/catalog')
55
require OctocatalogDiff::Spec.require_path('/catalog-diff/differ')
6+
require OctocatalogDiff::Spec.require_path('/errors')
67
require 'json'
78

89
# Read this about the fixtures:
@@ -61,7 +62,7 @@
6162
it 'should raise exception when something other than a catalog is passed in' do
6263
expect do
6364
OctocatalogDiff::CatalogDiff::Differ.new(@options, 'This is not a catalog!', @empty_puppet_catalog)
64-
end.to raise_error(OctocatalogDiff::CatalogDiff::Differ::DifferError)
65+
end.to raise_error(OctocatalogDiff::Errors::DifferError)
6566
end
6667
end
6768

spec/octocatalog-diff/tests/cli/printer_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative '../spec_helper'
44

55
require OctocatalogDiff::Spec.require_path('/cli/printer')
6+
require OctocatalogDiff::Spec.require_path('/errors')
67

78
require 'json'
89
require 'tempfile'
@@ -63,7 +64,7 @@
6364
testobj = OctocatalogDiff::Cli::Printer.new(opts, logger)
6465
expect do
6566
_result = testobj.printer(@diff)
66-
end.to raise_error(OctocatalogDiff::Cli::Printer::PrinterError)
67+
end.to raise_error(OctocatalogDiff::Errors::PrinterError)
6768

6869
# Make sure log messages are correct
6970
expect(logger_str.string).to match(/DEBUG -- : Generating non-colored text output/)

0 commit comments

Comments
 (0)