Skip to content

Commit 6aa1920

Browse files
author
Kevin Paulisse
committed
Move CatalogError, BootstrapError to error class
1 parent 7a1673f commit 6aa1920

9 files changed

Lines changed: 48 additions & 41 deletions

File tree

doc/dev/api/v1/catalog.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Overview
44

5-
`catalog` allows you to build a catalog using the octocatalog-diff compiler or to obtain a catalog from a Puppet server.
5+
`catalog` returns an `OctocatalogDiff::Catalog` object built with the octocatalog-diff compiler, obtained from a Puppet server, or read in from a file. This is analogous to using the `--catalog-only` option with the octocatalog-diff command-line script.
66

7-
This is analogous to using the `--catalog-only` option with the octocatalog-diff command-line script.
7+
```
8+
catalog_obj = OctocatalogDiff::API::V1.catalog(
9+
filename: "String",
10+
logger: Logger,
11+
test: <true|false>
12+
)
13+
```

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative '../bootstrap'
4+
require_relative '../errors'
45
require_relative '../util/parallel'
56
require_relative 'git'
67

@@ -12,10 +13,6 @@ module CatalogUtil
1213
# parallelization of bootstrap, and formats arguments as expected by the higher level bootstrap
1314
# script.
1415
class Bootstrap
15-
# Exceptions that are anticipated can be caught in the calling class and tested
16-
# for explicitly in spec tests.
17-
class BootstrapError < RuntimeError; end
18-
1916
# Bootstrap directories specified by --bootstrapped-from-dir and --bootstrapped-to-dir
2017
# command line options. Bootstrapping occurs in parallel. This takes no parameters (options come
2118
# from options) and returns nothing (it raises an exception if something fails).
@@ -28,7 +25,7 @@ def self.bootstrap_directory_parallelizer(options, logger)
2825
message = 'Must specify a from-branch other than . when using --bootstrapped-from-dir!' \
2926
' Please use "-f <from_branch>" argument.'
3027
logger.error(message)
31-
raise BootstrapError, message
28+
raise OctocatalogDiff::Errors::BootstrapError, message
3229
end
3330

3431
opts = options.merge(branch: options[:from_env],
@@ -43,7 +40,7 @@ def self.bootstrap_directory_parallelizer(options, logger)
4340
message = 'Must specify a to-branch other than . when using --bootstrapped-to-dir!' \
4441
' Please use "-t <to_branch>" argument.'
4542
logger.error(message)
46-
raise BootstrapError, message
43+
raise OctocatalogDiff::Errors::BootstrapError, message
4744
end
4845

4946
opts = options.merge(branch: options[:to_env],
@@ -58,7 +55,7 @@ def self.bootstrap_directory_parallelizer(options, logger)
5855
message = 'Specify one or more of --bootstrapped-from-dir / --bootstrapped-to-dir / --cached-master-dir' \
5956
' when using --bootstrap_then_exit'
6057
logger.error(message)
61-
raise BootstrapError, message
58+
raise OctocatalogDiff::Errors::BootstrapError, message
6259
end
6360

6461
# Bootstrap the directories in parallel. Since there are no results here that we
@@ -78,7 +75,7 @@ def self.bootstrap_directory_parallelizer(options, logger)
7875
logger.debug("Success bootstrap_directory for #{result.args[:tag]}")
7976
else
8077
errmsg = "Failed bootstrap_directory for #{result.args[:tag]}: #{result.exception.class} #{result.exception.message}"
81-
raise BootstrapError, errmsg
78+
raise OctocatalogDiff::Errors::BootstrapError, errmsg
8279
end
8380
end
8481
end
@@ -106,7 +103,7 @@ def self.git_checkout(logger, dir_opts)
106103
logger.debug("Success git checkout #{dir_opts[:basedir]}:#{dir_opts[:branch]} -> #{dir_opts[:path]}")
107104
rescue OctocatalogDiff::CatalogUtil::Git::GitCheckoutError => exc
108105
logger.error("Git checkout error: #{exc}")
109-
raise BootstrapError, exc
106+
raise OctocatalogDiff::Errors::BootstrapError, exc
110107
end
111108

112109
# Install bootstrap script in the target directory. This allows proper bootstrapping from the
@@ -121,7 +118,7 @@ def self.install_bootstrap_script(logger, opts)
121118
else
122119
File.join(opts[:basedir], opts[:bootstrap_script])
123120
end
124-
raise BootstrapError, "Bootstrap script #{src} does not exist" unless File.file?(src)
121+
raise OctocatalogDiff::Errors::BootstrapError, "Bootstrap script #{src} does not exist" unless File.file?(src)
125122

126123
logger.debug('Begin install bootstrap script in target directory')
127124

@@ -146,7 +143,9 @@ def self.run_bootstrap(logger, opts)
146143
output = result[:output].split(/[\r\n]+/)
147144
output.each { |x| logger.debug("Bootstrap: #{x}") }
148145
end
149-
raise BootstrapError, "bootstrap failed for #{opts[:path]}: #{result[:output]}" unless (result[:status_code]).zero?
146+
unless (result[:status_code]).zero?
147+
raise OctocatalogDiff::Errors::BootstrapError, "bootstrap failed for #{opts[:path]}: #{result[:output]}"
148+
end
150149
logger.debug("Success bootstrap in #{opts[:path]}")
151150
result[:output]
152151
end

lib/octocatalog-diff/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative 'api/v1/catalog-compile'
44
require_relative 'api/v1/catalog-diff'
55
require_relative 'catalog-util/cached_master_directory'
6+
require_relative 'errors'
67
require_relative 'util/catalogs'
78
require_relative 'version'
89

@@ -176,7 +177,7 @@ def self.catalog_only(logger, options)
176177
def self.bootstrap_then_exit(logger, catalogs_obj)
177178
catalogs_obj.bootstrap_then_exit
178179
return EXITCODE_SUCCESS_NO_DIFFS
179-
rescue OctocatalogDiff::Util::Catalogs::BootstrapError => exc
180+
rescue OctocatalogDiff::Errors::BootstrapError => exc
180181
logger.fatal("--bootstrap-then-exit error: bootstrap failed (#{exc})")
181182
return EXITCODE_FAILURE
182183
end

lib/octocatalog-diff/errors.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
module OctocatalogDiff
44
# Contains error classes raised by this gem
55
class Errors
6-
# Error class for handled configuration file errors
6+
# Error classes for handled configuration file errors
77
class ConfigurationFileNotFoundError < RuntimeError; end
88
class ConfigurationFileContentError < RuntimeError; end
9+
10+
# Error classes for building catalogs
11+
class BootstrapError < RuntimeError; end
12+
class CatalogError < RuntimeError; end
913
end
1014
end

lib/octocatalog-diff/util/catalogs.rb

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
require 'json'
44
require 'open3'
55
require 'yaml'
6-
require_relative '../catalog-util/bootstrap' # For BootstrapError
76
require_relative '../catalog'
7+
require_relative '../errors'
88
require_relative 'parallel'
99

1010
module OctocatalogDiff
1111
module Util
1212
# Helper class to construct catalogs, performing all necessary steps such as
1313
# bootstrapping directories, installing facts, and running puppet.
1414
class Catalogs
15-
# Exceptions that are anticipated can be caught in the calling class and tested for explicitly in spec tests.
16-
class BootstrapError < RuntimeError; end
17-
class CatalogError < RuntimeError; end
18-
1915
# Constructor
2016
# @param options [Hash] Options
2117
# @param logger [Logger] Logger object
@@ -40,9 +36,6 @@ def bootstrap_then_exit
4036
OctocatalogDiff::CatalogUtil::Bootstrap.bootstrap_directory_parallelizer(@options, @logger)
4137
@logger.debug('Success bootstrap_then_exit')
4238
@logger.info('Successfully completed --bootstrap-then-exit action')
43-
rescue OctocatalogDiff::CatalogUtil::Bootstrap::BootstrapError => exc
44-
@logger.error("Bootstrap exception: #{exc}")
45-
raise BootstrapError, "Bootstrap exception: #{exc}"
4639
end
4740

4841
private
@@ -121,8 +114,8 @@ def build_catalog_parallelizer
121114

122115
# Things have succeeded if the :to and :from catalogs exist at this point. If not, things have
123116
# failed, and an exception should be thrown.
124-
raise CatalogError, 'One or more catalogs failed to compile.' unless result.key?(:to) && result.key?(:from)
125-
result
117+
return result if result.key?(:to) && result.key?(:from)
118+
raise OctocatalogDiff::Errors::CatalogError, 'One or more catalogs failed to compile.'
126119
end
127120

128121
# Get catalog compilation tasks.
@@ -201,14 +194,15 @@ def add_parallel_result(result, parallel_catalog_obj, key_task_tuple)
201194
error_display = catalog.error_message.split("\n").map do |line|
202195
line.sub(/^Error:/, '[Puppet Error]').gsub(dir_regex, '')
203196
end.join("\n")
204-
raise CatalogError, "Catalog for #{branch} failed to compile due to errors:\n#{error_display}"
197+
message = "Catalog for #{branch} failed to compile due to errors:\n#{error_display}"
198+
raise OctocatalogDiff::Errors::CatalogError, message
205199
end
206200
else
207201
# Something unhandled went wrong, and an exception was thrown. Reveal a generic message.
208202
msg = parallel_catalog_obj.exception.message
209203
message = "Catalog for '#{key}' (#{branch}) failed to compile with #{parallel_catalog_obj.exception.class}: #{msg}"
210204
message += "\n" + parallel_catalog_obj.exception.backtrace.map { |x| " #{x}" }.join("\n") if @options[:debug]
211-
raise CatalogError, message
205+
raise OctocatalogDiff::Errors::CatalogError, message
212206
end
213207
end
214208

spec/octocatalog-diff/integration/bootstrap_script_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
it 'should print error from bootstrap failing' do
5555
expect(@result[:exception].message).to match(/Catalog for 'to' \(.\) failed to compile with.+::BootstrapError/)
56-
expect(@result[:exception].message).to match(/OctocatalogDiff::CatalogUtil::Bootstrap::BootstrapError/)
56+
expect(@result[:exception].message).to match(/OctocatalogDiff::Errors::BootstrapError/)
5757
expect(@result[:exception].message).not_to match(/Could not find class (::)?test/)
5858
end
5959

spec/octocatalog-diff/tests/catalog-util/bootstrap_spec.rb

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

55
require OctocatalogDiff::Spec.require_path('/catalog-util/bootstrap')
6+
require OctocatalogDiff::Spec.require_path('/errors')
67

78
require 'fileutils'
89

@@ -25,7 +26,7 @@
2526
}
2627
expect do
2728
OctocatalogDiff::CatalogUtil::Bootstrap.bootstrap_directory_parallelizer(opts, logger)
28-
end.to raise_error(OctocatalogDiff::CatalogUtil::Bootstrap::BootstrapError, /Must specify a from-branch/)
29+
end.to raise_error(OctocatalogDiff::Errors::BootstrapError, /Must specify a from-branch/)
2930
expect(logger_str.string).to match(/Must specify a from-branch/)
3031
end
3132

@@ -38,7 +39,7 @@
3839
}
3940
expect do
4041
OctocatalogDiff::CatalogUtil::Bootstrap.bootstrap_directory_parallelizer(opts, logger)
41-
end.to raise_error(OctocatalogDiff::CatalogUtil::Bootstrap::BootstrapError, /Must specify a to-branch/)
42+
end.to raise_error(OctocatalogDiff::Errors::BootstrapError, /Must specify a to-branch/)
4243
expect(logger_str.string).to match(/Must specify a to-branch/)
4344
end
4445

@@ -47,7 +48,7 @@
4748
opts = {}
4849
expect do
4950
OctocatalogDiff::CatalogUtil::Bootstrap.bootstrap_directory_parallelizer(opts, logger)
50-
end.to raise_error(OctocatalogDiff::CatalogUtil::Bootstrap::BootstrapError, /Specify one or more of/)
51+
end.to raise_error(OctocatalogDiff::Errors::BootstrapError, /Specify one or more of/)
5152
expect(logger_str.string).to match(/Specify one or more of/)
5253
end
5354

spec/octocatalog-diff/tests/cli_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')
6+
require OctocatalogDiff::Spec.require_path('/errors')
67

78
describe OctocatalogDiff::Cli do
89
describe '#parse_opts' do
@@ -256,7 +257,7 @@
256257

257258
it 'should fail and exit 1 if BootstrapError occurs' do
258259
d = double('OctocatalogDiff::Util::Catalogs')
259-
allow(d).to receive(:bootstrap_then_exit).and_raise(OctocatalogDiff::Util::Catalogs::BootstrapError, 'hello')
260+
allow(d).to receive(:bootstrap_then_exit).and_raise(OctocatalogDiff::Errors::BootstrapError, 'hello')
260261
logger, logger_str = OctocatalogDiff::Spec.setup_logger
261262
rc = OctocatalogDiff::Cli.bootstrap_then_exit(logger, d)
262263
expect(rc).to eq(1)

spec/octocatalog-diff/tests/util/catalogs_spec.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_relative '../../mocks/puppetdb'
55

66
require OctocatalogDiff::Spec.require_path('/catalog')
7+
require OctocatalogDiff::Spec.require_path('/errors')
78
require OctocatalogDiff::Spec.require_path('/facts')
89
require OctocatalogDiff::Spec.require_path('/util/catalogs')
910

@@ -92,7 +93,7 @@ def valid?
9293
options = @default_options.merge(from_env: 'master')
9394
logger, logger_string = OctocatalogDiff::Spec.setup_logger
9495
testobj = OctocatalogDiff::Util::Catalogs.new(options, logger)
95-
expect { testobj.bootstrap_then_exit }.to raise_error(OctocatalogDiff::Util::Catalogs::BootstrapError)
96+
expect { testobj.bootstrap_then_exit }.to raise_error(OctocatalogDiff::Errors::BootstrapError)
9697
expect(logger_string.string).to match(%r{Specify one or more of --bootstrapped-from-dir / --bootstrapped-to-dir})
9798
end
9899

@@ -105,8 +106,8 @@ def valid?
105106
options = @default_options.merge(basedir: tmpdir2, bootstrapped_from_dir: tmpdir1, from_env: 'asdfasdfasdf')
106107
logger, logger_string = OctocatalogDiff::Spec.setup_logger
107108
testobj = OctocatalogDiff::Util::Catalogs.new(options, logger)
108-
expect { testobj.bootstrap_then_exit }.to raise_error(OctocatalogDiff::Util::Catalogs::BootstrapError)
109-
expect(logger_string.string).to match(/ERROR -- : Bootstrap exception: Failed bootstrap_directory for from_dir/)
109+
expect { testobj.bootstrap_then_exit }.to raise_error(OctocatalogDiff::Errors::BootstrapError)
110+
expect(logger_string.string).to match(/DEBUG .+ Failed bootstrap from_dir/)
110111
ensure
111112
OctocatalogDiff::Spec.clean_up_tmpdir(tmpdir1)
112113
OctocatalogDiff::Spec.clean_up_tmpdir(tmpdir2)
@@ -121,8 +122,8 @@ def valid?
121122
options = @default_options.merge(bootstrapped_from_dir: tmpdir1, from_env: 'asdfasdfasdf')
122123
logger, logger_string = OctocatalogDiff::Spec.setup_logger
123124
testobj = OctocatalogDiff::Util::Catalogs.new(options, logger)
124-
expect { testobj.bootstrap_then_exit }.to raise_error(OctocatalogDiff::Util::Catalogs::BootstrapError)
125-
expect(logger_string.string).to match(/ERROR -- : Bootstrap exception: Failed bootstrap_directory for from_dir/)
125+
expect { testobj.bootstrap_then_exit }.to raise_error(OctocatalogDiff::Errors::BootstrapError)
126+
expect(logger_string.string).to match(/DEBUG .+ Failed bootstrap from_dir/)
126127
ensure
127128
OctocatalogDiff::Spec.clean_up_tmpdir(tmpdir1)
128129
end
@@ -143,7 +144,7 @@ def valid?
143144
@bootstrap_error_message = nil
144145
ENV['PATH'] = '/usr/sbin:/sbin:/usr/bin:/bin:/usr/local/bin:/usr/local/sbin'
145146
testobj.bootstrap_then_exit
146-
rescue OctocatalogDiff::Util::Catalogs::BootstrapError => exc
147+
rescue OctocatalogDiff::Errors::BootstrapError => exc
147148
@bootstrap_error_message = "BootstrapError #{exc}: #{logger_string.string}"
148149
ensure
149150
ENV['PATH'] = path_save
@@ -197,7 +198,7 @@ def valid?
197198
logger, _logger_string = OctocatalogDiff::Spec.setup_logger
198199
testobj = OctocatalogDiff::Util::Catalogs.new(options, logger)
199200
re = %r{ENC.*/asdkfjlfjkalksdfads wasn't found}
200-
expect { testobj.catalogs }.to raise_error(OctocatalogDiff::Util::Catalogs::CatalogError, re)
201+
expect { testobj.catalogs }.to raise_error(OctocatalogDiff::Errors::CatalogError, re)
201202
end
202203
end
203204
end
@@ -210,7 +211,7 @@ def valid?
210211
logger, _logger_string = OctocatalogDiff::Spec.setup_logger
211212
testobj = OctocatalogDiff::Util::Catalogs.new(options, logger)
212213
re = %r{hiera.yaml.*/asdkfjlfjkalksdfads\) wasn't found}
213-
expect { testobj.catalogs }.to raise_error(OctocatalogDiff::Util::Catalogs::CatalogError, re)
214+
expect { testobj.catalogs }.to raise_error(OctocatalogDiff::Errors::CatalogError, re)
214215
end
215216
end
216217
end
@@ -343,7 +344,7 @@ def valid?
343344
answer = Regexp.new(lines.join('(.|\n)*'))
344345
expect do
345346
testobj.send(:add_parallel_result, result, pcr, key_task_tuple)
346-
end.to raise_error(OctocatalogDiff::Util::Catalogs::CatalogError, answer)
347+
end.to raise_error(OctocatalogDiff::Errors::CatalogError, answer)
347348

348349
expect(logger_str.string).to eq('')
349350
end

0 commit comments

Comments
 (0)