Skip to content

Commit 15bcd92

Browse files
committed
feat: adding log for not found configuration
1 parent 3909ee0 commit 15bcd92

File tree

9 files changed

+45
-59
lines changed

9 files changed

+45
-59
lines changed

.sourcelevel.yml

Lines changed: 0 additions & 34 deletions
This file was deleted.

Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
tshield (0.14.0.0)
4+
tshield (0.15.0.0)
55
grpc (~> 1.28, >= 1.28.0)
66
grpc-tools (~> 1.28, >= 1.28.0)
77
httparty (~> 0.14, >= 0.14.0)
@@ -59,10 +59,10 @@ GEM
5959
ffi (1.15.1)
6060
formatador (0.2.5)
6161
gherkin (5.1.0)
62-
google-protobuf (3.19.2)
62+
google-protobuf (3.19.2-x86_64-linux)
6363
googleapis-common-protos-types (1.0.6)
6464
google-protobuf (~> 3.14)
65-
grpc (1.38.0)
65+
grpc (1.38.0-x86_64-linux)
6666
google-protobuf (~> 3.15)
6767
googleapis-common-protos-types (~> 1.0)
6868
grpc-tools (1.38.0)

config/tshield.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
---
22
grpc:
33
port: 5678
4-
proto_dir: 'proto'
4+
proto_dir: "proto"
55
services:
6-
'helloworld_services_pb':
7-
module: 'Helloworld::Greeter'
8-
hostname: '0.0.0.0:50051'
6+
"helloworld_services_pb":
7+
module: "Helloworld::Greeter"
8+
hostname: "0.0.0.0:50051"
99
request:
1010
timeout: 10
1111
domains:
12-
'http://localhost:9090':
13-
name: 'components'
12+
"http://localhost:9090":
13+
name: "components"
1414
skip_query_params:
1515
- b
1616
paths:

lib/tshield/configuration.rb

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

33
require 'yaml'
44

5+
require 'tshield/errors'
56
require 'tshield/after_filter'
67
require 'tshield/before_filter'
78
require 'tshield/options'
@@ -59,7 +60,7 @@ def get_domain_for(path)
5960
result = self.class.get_url_for_domain_by_path(path, config)
6061
return url if result
6162
end
62-
nil
63+
raise ConfigurationNotFoundError.new("Domain not found for path #{path}")
6364
end
6465

6566
def windows_compatibility?
@@ -89,8 +90,12 @@ def cache_request?(domain)
8990
end
9091

9192
def get_filters(domain)
92-
(domains[domain]['filters'] || [])
93-
.collect { |filter| Class.const_get(filter) }
93+
begin
94+
(domains[domain]['filters'] || [])
95+
.collect { |filter| Class.const_get(filter) }
96+
rescue
97+
puts "Error loading filters for domain #{domain}"
98+
end
9499
end
95100

96101
def get_excluded_headers(domain)

lib/tshield/controllers/requests.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ def treat(params, request, _response)
5757
ip: request.ip
5858
}
5959

60-
treat_headers_by_domain(options, path)
61-
6260
if %w[POST PUT PATCH].include? method
6361
result = request.body.read.encode('UTF-8',
6462
invalid: :replace,
@@ -67,13 +65,21 @@ def treat(params, request, _response)
6765
options[:body] = result
6866
end
6967
api_response = TShield::RequestMatching.new(path, options.clone).match_request
70-
7168
unless api_response
72-
add_headers(options, path)
73-
74-
api_response ||= TShield::RequestVCR.new(path, options.clone).vcr_response
75-
api_response.headers.reject! do |key, _v|
76-
configuration.get_excluded_headers(domain(path)).include?(key)
69+
begin
70+
treat_headers_by_domain(options, path)
71+
add_headers(options, path)
72+
73+
api_response ||= TShield::RequestVCR.new(path, options.clone).vcr_response
74+
api_response.headers.reject! do |key, _v|
75+
configuration.get_excluded_headers(domain(path)).include?(key)
76+
end
77+
rescue ConfigurationNotFoundError => e
78+
logger.error("Error on recover configuration for #{path}")
79+
80+
status 500
81+
body({tshield: e }.to_json)
82+
return
7783
end
7884
end
7985

@@ -110,9 +116,13 @@ def domain(path)
110116
end
111117

112118
def delay(path)
113-
delay_in_seconds = configuration.get_delay(domain(path), path) || 0
114-
logger.info("Response with delay of #{delay_in_seconds} seconds")
115-
sleep delay_in_seconds
119+
begin
120+
delay_in_seconds = configuration.get_delay(domain(path), path) || 0
121+
logger.info("Response with delay of #{delay_in_seconds} seconds")
122+
sleep delay_in_seconds
123+
rescue ConfigurationNotFoundError
124+
logger.debug('No delay configured')
125+
end
116126
end
117127
end
118128
end

lib/tshield/errors.rb

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

33
class AppendSessionWithoutMainSessionError < RuntimeError
44
end
5+
6+
class ConfigurationNotFoundError < RuntimeError
7+
end
8+

lib/tshield/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module TShield
44
# Control version of gem
55
class Version
66
MAJOR = 0
7-
MINOR = 14
7+
MINOR = 15
88
PATCH = 0
99
PRE = 0
1010

spec/tshield/configuration_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
end
6262

6363
it 'return nil if domain not found' do
64-
expect(@configuration.get_domain_for('/api/four')).to be_nil
64+
expect{@configuration.get_domain_for('/api/four')}.to raise_error(ConfigurationNotFoundError)
6565
end
6666
end
6767

spec/tshield/controllers/requests_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def body(_value); end
4747
allow(matched_response).to receive(:headers).and_return({})
4848
allow(matched_response).to receive(:body).and_return('')
4949
allow(@mock_logger).to receive(:info)
50+
allow(@mock_logger).to receive(:debug)
5051

5152
expect(TShield::Controllers::Helpers::SessionHelpers)
5253
.to receive(:current_session_call).with(request, '/?a=b', 'GET')

0 commit comments

Comments
 (0)