Skip to content

Commit 72e7264

Browse files
author
Kevin Paulisse
committed
Move fact source and retrieval errors to error class
1 parent 880f279 commit 72e7264

6 files changed

Lines changed: 20 additions & 16 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative 'pe/v1'
44
require_relative '../../util/httparty'
5+
require_relative '../../errors'
56
require_relative '../facts'
67

78
module OctocatalogDiff
@@ -87,7 +88,7 @@ def facts(logger)
8788
begin
8889
result = facts_obj.facts
8990
logger.debug "Success retrieving facts for #{@node} from #{self.class}"
90-
rescue OctocatalogDiff::Facts::FactRetrievalError, OctocatalogDiff::Facts::FactSourceError => exc
91+
rescue OctocatalogDiff::Errors::FactRetrievalError, OctocatalogDiff::Errors::FactSourceError => exc
9192
@content = nil
9293
@error_message = "Fact retrieval failed: #{exc.class} - #{exc.message}"
9394
logger.error @error_message

lib/octocatalog-diff/errors.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ class ConfigurationFileContentError < RuntimeError; end
1010
# Error classes for building catalogs
1111
class BootstrapError < RuntimeError; end
1212
class CatalogError < RuntimeError; end
13+
14+
# Error classes for retrieving facts
15+
class FactSourceError < RuntimeError; end
16+
class FactRetrievalError < RuntimeError; end
1317
end
1418
end

lib/octocatalog-diff/facts.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require_relative 'errors'
34
require_relative 'facts/json'
45
require_relative 'facts/yaml'
56
require_relative 'facts/puppetdb'
@@ -119,9 +120,5 @@ def override(key, value)
119120
@facts['values'][key] = value
120121
end
121122
end
122-
123-
# Separate classes to handle errors we throw explicitly
124-
class FactSourceError < RuntimeError; end
125-
class FactRetrievalError < RuntimeError; end
126123
end
127124
end

lib/octocatalog-diff/facts/puppetdb.rb

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

3+
require_relative '../errors'
34
require_relative '../facts'
45
require_relative '../puppetdb'
56
require 'yaml'
@@ -42,21 +43,21 @@ def self.fact_retriever(options = {}, node)
4243
result.map { |x| facts[x['name']] = x['value'] }
4344
if facts.empty?
4445
message = "Unable to retrieve facts for node #{node} from PuppetDB (empty or nil)!"
45-
raise OctocatalogDiff::Facts::FactRetrievalError, message
46+
raise OctocatalogDiff::Errors::FactRetrievalError, message
4647
end
4748

4849
# Create a structure compatible with YAML fact files.
4950
obj_to_return = { 'name' => node, 'values' => {} }
5051
facts.each { |k, v| obj_to_return['values'][k.sub(/^::/, '')] = v }
5152
break # Not return, to avoid LocalJumpError in Ruby 2.2
5253
rescue OctocatalogDiff::PuppetDB::ConnectionError => exc
53-
exception_class = OctocatalogDiff::Facts::FactSourceError
54+
exception_class = OctocatalogDiff::Errors::FactSourceError
5455
exception_message = "Fact retrieval failed (#{exc.class}) (#{exc.message})"
5556
rescue OctocatalogDiff::PuppetDB::NotFoundError => exc
56-
exception_class = OctocatalogDiff::Facts::FactRetrievalError
57+
exception_class = OctocatalogDiff::Errors::FactRetrievalError
5758
exception_message = "Node #{node} not found in PuppetDB (#{exc.message})"
5859
rescue OctocatalogDiff::PuppetDB::PuppetDBError => exc
59-
exception_class = OctocatalogDiff::Facts::FactRetrievalError
60+
exception_class = OctocatalogDiff::Errors::FactRetrievalError
6061
exception_message = "Fact retrieval failed for node #{node} from PuppetDB (#{exc.message})"
6162
end
6263
end

spec/octocatalog-diff/tests/catalog-util/enc/pe_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@
200200
@logger, @logger_str = OctocatalogDiff::Spec.setup_logger
201201

202202
fact_obj = double('OctocatalogDiff::CatalogUtil::Facts')
203-
allow(fact_obj).to receive(:facts).and_raise(OctocatalogDiff::Facts::FactRetrievalError, 'Fact Error')
203+
allow(fact_obj).to receive(:facts).and_raise(OctocatalogDiff::Errors::FactRetrievalError, 'Fact Error')
204204
allow(OctocatalogDiff::CatalogUtil::Facts).to receive(:new).and_return(fact_obj)
205205

206206
opts = { node: 'foo', pe_enc_url: 'https://localhost:4433/classifier-api' }
@@ -213,15 +213,15 @@
213213
end
214214

215215
it 'should set error message' do
216-
answer = 'Fact retrieval failed: OctocatalogDiff::Facts::FactRetrievalError - Fact Error'
216+
answer = 'Fact retrieval failed: OctocatalogDiff::Errors::FactRetrievalError - Fact Error'
217217
expect(@testobj.error_message).to eq(answer)
218218
end
219219

220220
it 'should log messages' do
221221
log = @logger_str.string
222222
expect(log).to match(/DEBUG.+Beginning OctocatalogDiff::CatalogUtil::ENC::PE#execute for foo/)
223223
expect(log).to match(/DEBUG.+Start retrieving facts for foo from OctocatalogDiff::CatalogUtil::ENC::PE/)
224-
expect(log).to match(/ERROR.+Fact retrieval failed: OctocatalogDiff::Facts::FactRetrievalError - Fact Error/)
224+
expect(log).to match(/ERROR.+Fact retrieval failed: OctocatalogDiff::Errors::FactRetrievalError - Fact Error/)
225225
end
226226
end
227227
end

spec/octocatalog-diff/tests/facts/puppetdb_spec.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative '../spec_helper'
44
require_relative '../../mocks/puppetdb'
5+
require OctocatalogDiff::Spec.require_path('/errors')
56
require OctocatalogDiff::Spec.require_path('/facts/puppetdb')
67

78
describe OctocatalogDiff::Facts::PuppetDB do
@@ -27,7 +28,7 @@
2728
node = 'fjoaewjroisajdfoisdjfaojeworjsdofjsdofawejr'
2829
expect do
2930
OctocatalogDiff::Facts::PuppetDB.fact_retriever(@opts, node)
30-
end.to raise_error(OctocatalogDiff::Facts::FactRetrievalError)
31+
end.to raise_error(OctocatalogDiff::Errors::FactRetrievalError)
3132
end
3233
end
3334
end
@@ -85,7 +86,7 @@
8586
allow(OctocatalogDiff::PuppetDB).to receive(:new) { |*_arg| obj }
8687
expect do
8788
OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, node)
88-
end.to raise_error(OctocatalogDiff::Facts::FactSourceError, /Fact retrieval failed \(.*ConnectionError\) \(test/)
89+
end.to raise_error(OctocatalogDiff::Errors::FactSourceError, /Fact retrieval failed \(.*ConnectionError\) \(test/)
8990
end
9091

9192
it 'should handle OctocatalogDiff::PuppetDB::NotFoundError' do
@@ -94,7 +95,7 @@
9495
allow(OctocatalogDiff::PuppetDB).to receive(:new) { |*_arg| obj }
9596
expect do
9697
OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, node)
97-
end.to raise_error(OctocatalogDiff::Facts::FactRetrievalError, /Node valid-facts not found in PuppetDB \(test/)
98+
end.to raise_error(OctocatalogDiff::Errors::FactRetrievalError, /Node valid-facts not found in PuppetDB \(test/)
9899
end
99100

100101
it 'should handle OctocatalogDiff::PuppetDB::PuppetDBError' do
@@ -103,7 +104,7 @@
103104
allow(OctocatalogDiff::PuppetDB).to receive(:new) { |*_arg| obj }
104105
expect do
105106
OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, node)
106-
end.to raise_error(OctocatalogDiff::Facts::FactRetrievalError, /Fact retrieval failed for node valid-facts from PuppetDB/)
107+
end.to raise_error(OctocatalogDiff::Errors::FactRetrievalError, /Fact retrieval failed for node valid-facts/)
107108
end
108109
end
109110
end

0 commit comments

Comments
 (0)