Skip to content

Commit f868a7a

Browse files
author
Kevin Paulisse
committed
Centralize errors to their own class
1 parent 89cb1de commit f868a7a

4 files changed

Lines changed: 30 additions & 19 deletions

File tree

lib/octocatalog-diff.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# These are all the classes we believe people might want to call directly, so load
22
# them in response to a 'require octocatalog-diff'.
33

4-
loads = %w(api/v1 bootstrap catalog cli facts puppetdb version)
4+
loads = %w(api/v1 bootstrap catalog cli errors facts puppetdb version)
55
loads.each { |f| require_relative "octocatalog-diff/#{f}" }

lib/octocatalog-diff/api/v1/config.rb

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

33
require_relative 'common'
4+
require_relative '../../errors'
45

56
module OctocatalogDiff
67
module API
78
module V1
89
# This class interacts with the configuration file typically named `.octocatalog-diff.cfg.rb`.
910
class Config
10-
# Error class for handled configuration file errors
11-
class ConfigurationFileNotFoundError < RuntimeError; end
12-
class ConfigurationFileContentError < RuntimeError; end
13-
1411
# Default directory paths: These are the documented default locations that will be checked
1512
# for the configuration file.
1613
DEFAULT_PATHS = [
@@ -41,7 +38,7 @@ def self.config(options_in = {})
4138
# Can't find the configuration file?
4239
if config_file.nil?
4340
message = "Unable to find configuration file in #{paths.join(':')}"
44-
raise ConfigurationFileNotFoundError, message if options[:test]
41+
raise OctocatalogDiff::Errors::ConfigurationFileNotFoundError, message if options[:test]
4542
logger.debug message
4643
return {}
4744
end
@@ -89,18 +86,21 @@ def self.load_config_file(filename, logger)
8986
begin
9087
loaded_class = Kernel.const_get(:OctocatalogDiff).const_get(:Config)
9188
rescue NameError
92-
raise ConfigurationFileContentError, 'Configuration must define OctocatalogDiff::Config!'
89+
message = 'Configuration must define OctocatalogDiff::Config!'
90+
raise OctocatalogDiff::Errors::ConfigurationFileContentError, message
9391
end
9492

9593
unless loaded_class.respond_to?(:config)
96-
raise ConfigurationFileContentError, 'Configuration must define OctocatalogDiff::Config.config!'
94+
message = 'Configuration must define OctocatalogDiff::Config.config!'
95+
raise OctocatalogDiff::Errors::ConfigurationFileContentError, message
9796
end
9897

9998
# The configuration file looks like it defines the correct method, so read it.
10099
# Make sure it's a hash.
101100
options = loaded_class.config
102101
unless options.is_a?(Hash)
103-
raise ConfigurationFileContentError, "Configuration must be Hash not #{options.class}!"
102+
message = "Configuration must be Hash not #{options.class}!"
103+
raise OctocatalogDiff::Errors::ConfigurationFileContentError, message
104104
end
105105

106106
options

lib/octocatalog-diff/errors.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
module OctocatalogDiff
4+
# Contains error classes raised by this gem
5+
class Errors
6+
# Error class for handled configuration file errors
7+
class ConfigurationFileNotFoundError < RuntimeError; end
8+
class ConfigurationFileContentError < RuntimeError; end
9+
end
10+
end

spec/octocatalog-diff/tests/api/v1/config_spec.rb

Lines changed: 11 additions & 10 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('/api/v1/config')
6+
require OctocatalogDiff::Spec.require_path('/errors')
67

78
describe OctocatalogDiff::API::V1::Config do
89
before(:each) do
@@ -28,7 +29,7 @@
2829
expect(File).to receive(:'file?').with(filename).and_return(false)
2930
expect do
3031
described_class.config(logger: @logger, filename: filename, test: true)
31-
end.to raise_error(OctocatalogDiff::API::V1::Config::ConfigurationFileNotFoundError, pattern)
32+
end.to raise_error(OctocatalogDiff::Errors::ConfigurationFileNotFoundError, pattern)
3233
end
3334
end
3435

@@ -159,17 +160,17 @@
159160
pattern = Regexp.new('must define OctocatalogDiff::Config.config!')
160161
expect do
161162
described_class.load_config_file(filename, @logger)
162-
end.to raise_error(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError, pattern)
163+
end.to raise_error(OctocatalogDiff::Errors::ConfigurationFileContentError, pattern)
163164
end
164165

165166
it 'should log fatal message' do
166167
exception = nil
167168
begin
168169
described_class.load_config_file(filename, @logger)
169-
rescue OctocatalogDiff::API::V1::Config::ConfigurationFileContentError => exc
170+
rescue OctocatalogDiff::Errors::ConfigurationFileContentError => exc
170171
exception = exc
171172
end
172-
expect(exception).to be_a_kind_of(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError)
173+
expect(exception).to be_a_kind_of(OctocatalogDiff::Errors::ConfigurationFileContentError)
173174
expect(exception.message).to eq('Configuration must define OctocatalogDiff::Config.config!')
174175
expect(@logger_str.string).to match(/Configuration must define OctocatalogDiff::Config.config!/)
175176
end
@@ -182,17 +183,17 @@
182183
pattern = Regexp.new('must define OctocatalogDiff::Config!')
183184
expect do
184185
described_class.load_config_file(filename, @logger)
185-
end.to raise_error(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError, pattern)
186+
end.to raise_error(OctocatalogDiff::Errors::ConfigurationFileContentError, pattern)
186187
end
187188

188189
it 'should log fatal message' do
189190
exception = nil
190191
begin
191192
described_class.load_config_file(filename, @logger)
192-
rescue OctocatalogDiff::API::V1::Config::ConfigurationFileContentError => exc
193+
rescue OctocatalogDiff::Errors::ConfigurationFileContentError => exc
193194
exception = exc
194195
end
195-
expect(exception).to be_a_kind_of(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError)
196+
expect(exception).to be_a_kind_of(OctocatalogDiff::Errors::ConfigurationFileContentError)
196197
expect(exception.message).to eq('Configuration must define OctocatalogDiff::Config!')
197198
expect(@logger_str.string).to match(/Configuration must define OctocatalogDiff::Config!/)
198199
end
@@ -205,17 +206,17 @@
205206
pattern = Regexp.new('Configuration must be Hash not Array!')
206207
expect do
207208
described_class.load_config_file(filename, @logger)
208-
end.to raise_error(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError, pattern)
209+
end.to raise_error(OctocatalogDiff::Errors::ConfigurationFileContentError, pattern)
209210
end
210211

211212
it 'should log fatal message' do
212213
exception = nil
213214
begin
214215
described_class.load_config_file(filename, @logger)
215-
rescue OctocatalogDiff::API::V1::Config::ConfigurationFileContentError => exc
216+
rescue OctocatalogDiff::Errors::ConfigurationFileContentError => exc
216217
exception = exc
217218
end
218-
expect(exception).to be_a_kind_of(OctocatalogDiff::API::V1::Config::ConfigurationFileContentError)
219+
expect(exception).to be_a_kind_of(OctocatalogDiff::Errors::ConfigurationFileContentError)
219220
expect(exception.message).to eq('Configuration must be Hash not Array!')
220221
expect(@logger_str.string).to match(/Configuration must be Hash not Array!/)
221222
end

0 commit comments

Comments
 (0)