Skip to content

Commit 6689007

Browse files
author
Kevin Paulisse
committed
Move PuppetDB errors to error class
1 parent 72e7264 commit 6689007

8 files changed

Lines changed: 36 additions & 31 deletions

File tree

lib/octocatalog-diff/catalog/puppetdb.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'json'
44
require 'stringio'
55

6+
require_relative '../errors'
67
require_relative '../puppetdb'
78

89
module OctocatalogDiff
@@ -67,11 +68,11 @@ def fetch_catalog(logger)
6768
# Set the other variables
6869
@catalog_json = ::JSON.generate(@catalog)
6970
@error_message = nil
70-
rescue OctocatalogDiff::PuppetDB::ConnectionError => exc
71+
rescue OctocatalogDiff::Errors::PuppetDBConnectionError => exc
7172
@error_message = "Catalog retrieval failed (#{exc.class}) (#{exc.message})"
72-
rescue OctocatalogDiff::PuppetDB::NotFoundError => exc
73+
rescue OctocatalogDiff::Errors::PuppetDBNodeNotFoundError => exc
7374
@error_message = "Node #{node} not found in PuppetDB (#{exc.message})"
74-
rescue OctocatalogDiff::PuppetDB::PuppetDBError => exc
75+
rescue OctocatalogDiff::Errors::PuppetDBGenericError => exc
7576
@error_message = "Catalog retrieval failed for node #{node} from PuppetDB (#{exc.message})"
7677
rescue ::JSON::GeneratorError => exc
7778
@error_message = "Failed to generate result from PuppetDB as JSON (#{exc.message})"

lib/octocatalog-diff/errors.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ class CatalogError < RuntimeError; end
1414
# Error classes for retrieving facts
1515
class FactSourceError < RuntimeError; end
1616
class FactRetrievalError < RuntimeError; end
17+
18+
# Errors for PuppetDB
19+
class PuppetDBNodeNotFoundError < RuntimeError; end
20+
class PuppetDBGenericError < RuntimeError; end
21+
class PuppetDBConnectionError < RuntimeError; end
1722
end
1823
end

lib/octocatalog-diff/facts/puppetdb.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ def self.fact_retriever(options = {}, node)
5050
obj_to_return = { 'name' => node, 'values' => {} }
5151
facts.each { |k, v| obj_to_return['values'][k.sub(/^::/, '')] = v }
5252
break # Not return, to avoid LocalJumpError in Ruby 2.2
53-
rescue OctocatalogDiff::PuppetDB::ConnectionError => exc
53+
rescue OctocatalogDiff::Errors::PuppetDBConnectionError => exc
5454
exception_class = OctocatalogDiff::Errors::FactSourceError
5555
exception_message = "Fact retrieval failed (#{exc.class}) (#{exc.message})"
56-
rescue OctocatalogDiff::PuppetDB::NotFoundError => exc
56+
rescue OctocatalogDiff::Errors::PuppetDBNodeNotFoundError => exc
5757
exception_class = OctocatalogDiff::Errors::FactRetrievalError
5858
exception_message = "Node #{node} not found in PuppetDB (#{exc.message})"
59-
rescue OctocatalogDiff::PuppetDB::PuppetDBError => exc
59+
rescue OctocatalogDiff::Errors::PuppetDBGenericError => exc
6060
exception_class = OctocatalogDiff::Errors::FactRetrievalError
6161
exception_message = "Fact retrieval failed for node #{node} from PuppetDB (#{exc.message})"
6262
end

lib/octocatalog-diff/puppetdb.rb

Lines changed: 5 additions & 9 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 'util/httparty'
45

56
require 'uri'
@@ -14,11 +15,6 @@
1415
module OctocatalogDiff
1516
# A standard way to connect to PuppetDB from the various scripts in this repository.
1617
class PuppetDB
17-
# This class raises errors for certain handled problems
18-
class NotFoundError < RuntimeError; end
19-
class PuppetDBError < RuntimeError; end
20-
class ConnectionError < RuntimeError; end
21-
2218
# Allow connections to be read (used in tests for now)
2319
attr_reader :connections
2420

@@ -87,7 +83,7 @@ def initialize(options = {})
8783
def get(path)
8884
_get(path)
8985
rescue Net::OpenTimeout, Errno::ECONNREFUSED => exc
90-
raise ConnectionError, "#{exc.class} connecting to PuppetDB (need VPN on?): #{exc.message}"
86+
raise OctocatalogDiff::Errors::PuppetDBConnectionError, "#{exc.class} connecting to PuppetDB (need VPN on?): #{exc.message}"
9187
end
9288

9389
private
@@ -120,16 +116,16 @@ def _get(path)
120116

121117
# Handle all non-200's from PuppetDB
122118
unless response[:code] == 200
123-
raise NotFoundError, "404 - #{response[:error]}" if response[:code] == 404
124-
raise PuppetDBError, "#{response[:code]} - #{response[:error]}"
119+
raise OctocatalogDiff::Errors::PuppetDBNodeNotFoundError, "404 - #{response[:error]}" if response[:code] == 404
120+
raise OctocatalogDiff::Errors::PuppetDBGenericError, "#{response[:code]} - #{response[:error]}"
125121
end
126122

127123
# PuppetDB can return 'Not Found' as a string with a 200 response code
128124
raise NotFoundError, '404 - Not Found' if response[:body] == 'Not Found'
129125

130126
# PuppetDB can also return an error message in a 200; we'll call this a 500
131127
if response.key?(:error)
132-
raise PuppetDBError, "500 - #{response[:error]}"
128+
raise OctocatalogDiff::Errors::PuppetDBGenericError, "500 - #{response[:error]}"
133129
end
134130

135131
# If we get here without raising an error, it will fall out of the begin/rescue

spec/octocatalog-diff/mocks/puppetdb.rb

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

33
require_relative '../tests/spec_helper'
44

5+
require OctocatalogDiff::Spec.require_path('/errors')
56
require OctocatalogDiff::Spec.require_path('/puppetdb')
67

78
module OctocatalogDiff
@@ -59,7 +60,7 @@ def override_facts(hostname)
5960
# @return [String] JSON catalog
6061
def catalog(hostname)
6162
fixture_file = OctocatalogDiff::Spec.fixture_path(File.join('catalogs', "#{hostname}.json"))
62-
raise OctocatalogDiff::PuppetDB::NotFoundError, '404 - Not Found' unless File.file?(fixture_file)
63+
raise OctocatalogDiff::Errors::PuppetDBNodeNotFoundError, '404 - Not Found' unless File.file?(fixture_file)
6364
JSON.parse(File.read(fixture_file))
6465
end
6566
end

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require OctocatalogDiff::Spec.require_path('/catalog')
99
require OctocatalogDiff::Spec.require_path('/catalog/puppetdb')
1010
require OctocatalogDiff::Spec.require_path('/catalog-util/builddir')
11+
require OctocatalogDiff::Spec.require_path('/errors')
1112

1213
describe OctocatalogDiff::Catalog::PuppetDB do
1314
context 'with working catalog' do
@@ -106,16 +107,16 @@
106107

107108
it 'should set error message for connection error' do
108109
puppetdb = double('OctocatalogDiff::PuppetDB')
109-
allow(puppetdb).to receive(:get).and_raise(OctocatalogDiff::PuppetDB::ConnectionError, 'test')
110+
allow(puppetdb).to receive(:get).and_raise(OctocatalogDiff::Errors::PuppetDBConnectionError, 'test')
110111
allow(OctocatalogDiff::PuppetDB).to receive(:new).and_return(puppetdb)
111112

112113
@obj.send(:fetch_catalog, @logger)
113-
expect(@obj.error_message).to match(/Catalog retrieval failed \(.*::ConnectionError\) \(test\)/)
114+
expect(@obj.error_message).to match(/Catalog retrieval failed \(.*::PuppetDBConnectionError\) \(test\)/)
114115
end
115116

116117
it 'should set error message for not found error' do
117118
puppetdb = double('OctocatalogDiff::PuppetDB')
118-
allow(puppetdb).to receive(:get).and_raise(OctocatalogDiff::PuppetDB::NotFoundError, 'test')
119+
allow(puppetdb).to receive(:get).and_raise(OctocatalogDiff::Errors::PuppetDBNodeNotFoundError, 'test')
119120
allow(OctocatalogDiff::PuppetDB).to receive(:new).and_return(puppetdb)
120121

121122
@obj.send(:fetch_catalog, @logger)
@@ -124,7 +125,7 @@
124125

125126
it 'should set error message for puppetdb error' do
126127
puppetdb = double('OctocatalogDiff::PuppetDB')
127-
allow(puppetdb).to receive(:get).and_raise(OctocatalogDiff::PuppetDB::PuppetDBError, 'test')
128+
allow(puppetdb).to receive(:get).and_raise(OctocatalogDiff::Errors::PuppetDBGenericError, 'test')
128129
allow(OctocatalogDiff::PuppetDB).to receive(:new).and_return(puppetdb)
129130

130131
@obj.send(:fetch_catalog, @logger)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,27 @@
8080
let(:opts) { { puppetdb_url: 'https://mocked-puppetdb.somedomain.xyz:8081', node: 'valid-facts' } }
8181
let(:node) { 'valid-facts' }
8282

83-
it 'should handle OctocatalogDiff::PuppetDB::ConnectionError' do
83+
it 'should handle OctocatalogDiff::Errors::PuppetDBConnectionError' do
8484
obj = double('OctocatalogDiff::PuppetDB')
85-
allow(obj).to receive(:get).and_raise(OctocatalogDiff::PuppetDB::ConnectionError, 'test message')
85+
allow(obj).to receive(:get).and_raise(OctocatalogDiff::Errors::PuppetDBConnectionError, 'test message')
8686
allow(OctocatalogDiff::PuppetDB).to receive(:new) { |*_arg| obj }
8787
expect do
8888
OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, node)
8989
end.to raise_error(OctocatalogDiff::Errors::FactSourceError, /Fact retrieval failed \(.*ConnectionError\) \(test/)
9090
end
9191

92-
it 'should handle OctocatalogDiff::PuppetDB::NotFoundError' do
92+
it 'should handle OctocatalogDiff::Errors::PuppetDBNodeNotFoundError' do
9393
obj = double('OctocatalogDiff::PuppetDB')
94-
allow(obj).to receive(:get).and_raise(OctocatalogDiff::PuppetDB::NotFoundError, 'test message')
94+
allow(obj).to receive(:get).and_raise(OctocatalogDiff::Errors::PuppetDBNodeNotFoundError, 'test message')
9595
allow(OctocatalogDiff::PuppetDB).to receive(:new) { |*_arg| obj }
9696
expect do
9797
OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, node)
9898
end.to raise_error(OctocatalogDiff::Errors::FactRetrievalError, /Node valid-facts not found in PuppetDB \(test/)
9999
end
100100

101-
it 'should handle OctocatalogDiff::PuppetDB::PuppetDBError' do
101+
it 'should handle OctocatalogDiff::Errors::PuppetDBGenericError' do
102102
obj = double('OctocatalogDiff::PuppetDB')
103-
allow(obj).to receive(:get).and_raise(OctocatalogDiff::PuppetDB::PuppetDBError, 'test message')
103+
allow(obj).to receive(:get).and_raise(OctocatalogDiff::Errors::PuppetDBGenericError, 'test message')
104104
allow(OctocatalogDiff::PuppetDB).to receive(:new) { |*_arg| obj }
105105
expect do
106106
OctocatalogDiff::Facts::PuppetDB.fact_retriever(opts, node)

spec/octocatalog-diff/tests/puppetdb_spec.rb

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

33
# Test the OctocatalogDiff::PuppetDB library
44
require_relative 'spec_helper'
5+
require OctocatalogDiff::Spec.require_path('/errors')
56
require OctocatalogDiff::Spec.require_path('/puppetdb')
67
require 'uri'
78

@@ -174,7 +175,7 @@ def ssl_test(server_opts, opts = {})
174175
puppetdb_url: 'http://127.0.0.1:1'
175176
}
176177
testobj = OctocatalogDiff::PuppetDB.new(opts)
177-
expect { testobj.get('/foo') }.to raise_error(OctocatalogDiff::PuppetDB::ConnectionError)
178+
expect { testobj.get('/foo') }.to raise_error(OctocatalogDiff::Errors::PuppetDBConnectionError)
178179
end
179180

180181
it 'should timeout correctly' do
@@ -187,7 +188,7 @@ def ssl_test(server_opts, opts = {})
187188
}
188189
testobj = OctocatalogDiff::PuppetDB.new(opts)
189190
time_begin = Time.now.to_i
190-
expect { testobj.get('/foo') }.to raise_error(OctocatalogDiff::PuppetDB::ConnectionError)
191+
expect { testobj.get('/foo') }.to raise_error(OctocatalogDiff::Errors::PuppetDBConnectionError)
191192
time_end = Time.now.to_i
192193
expect(time_end - time_begin).to be <= 5
193194
end
@@ -463,17 +464,17 @@ def ssl_test(server_opts, opts = {})
463464

464465
it 'should handle 404 responses' do
465466
allow(OctocatalogDiff::Util::HTTParty).to receive(:get).and_return(code: 404, error: 'oh noez')
466-
expect { @testobj.send(:get, '/foo') }.to raise_error(OctocatalogDiff::PuppetDB::NotFoundError, /oh noez/)
467+
expect { @testobj.send(:get, '/foo') }.to raise_error(OctocatalogDiff::Errors::PuppetDBNodeNotFoundError, /oh noez/)
467468
end
468469

469470
it 'should handle !=200, !=404 responses' do
470471
allow(OctocatalogDiff::Util::HTTParty).to receive(:get).and_return(code: 499, error: 'oh noez')
471-
expect { @testobj.send(:get, '/foo') }.to raise_error(OctocatalogDiff::PuppetDB::PuppetDBError, /oh noez/)
472+
expect { @testobj.send(:get, '/foo') }.to raise_error(OctocatalogDiff::Errors::PuppetDBGenericError, /oh noez/)
472473
end
473474

474475
it 'should handle responses with :error' do
475476
allow(OctocatalogDiff::Util::HTTParty).to receive(:get).and_return(code: 200, error: 'oh noez')
476-
expect { @testobj.send(:get, '/foo') }.to raise_error(OctocatalogDiff::PuppetDB::PuppetDBError, /500 - oh noez/)
477+
expect { @testobj.send(:get, '/foo') }.to raise_error(OctocatalogDiff::Errors::PuppetDBGenericError, /500 - oh noez/)
477478
end
478479

479480
it 'should handle unparseable responses' do

0 commit comments

Comments
 (0)