sanford 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,8 +9,8 @@ module Sanford
|
|
9
9
|
|
10
10
|
def initialize(exception, host_data = nil, request = nil)
|
11
11
|
@exception, @host_data, @request = exception, host_data, request
|
12
|
-
@keep_alive
|
13
|
-
@
|
12
|
+
@keep_alive = @host_data ? @host_data.keep_alive : false
|
13
|
+
@error_procs = @host_data ? @host_data.error_procs.reverse : []
|
14
14
|
end
|
15
15
|
|
16
16
|
# The exception that we are generating a response for can change in the case
|
@@ -20,12 +20,17 @@ module Sanford
|
|
20
20
|
# server will respond and log based on the last exception that occurred.
|
21
21
|
|
22
22
|
def run
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
response = nil
|
24
|
+
@error_procs.each do |error_proc|
|
25
|
+
result = nil
|
26
|
+
begin
|
27
|
+
result = error_proc.call(@exception, @host_data, @request)
|
28
|
+
rescue Exception => proc_exception
|
29
|
+
@exception = proc_exception
|
30
|
+
end
|
31
|
+
response ||= self.response_from_proc(result)
|
27
32
|
end
|
28
|
-
|
33
|
+
response || self.response_from_exception(@exception)
|
29
34
|
end
|
30
35
|
|
31
36
|
protected
|
data/lib/sanford/host.rb
CHANGED
@@ -26,7 +26,7 @@ module Sanford
|
|
26
26
|
option :verbose_logging, :default => true
|
27
27
|
option :receives_keep_alive, :default => false
|
28
28
|
option :runner, :default => proc{ Sanford.config.runner }
|
29
|
-
option :
|
29
|
+
option :error_procs, Array, :default => []
|
30
30
|
option :init_proc, Proc, :default => proc{ }
|
31
31
|
|
32
32
|
def initialize(host)
|
@@ -83,7 +83,7 @@ module Sanford
|
|
83
83
|
end
|
84
84
|
|
85
85
|
def error(&block)
|
86
|
-
self.configuration.
|
86
|
+
self.configuration.error_procs << block
|
87
87
|
end
|
88
88
|
|
89
89
|
def init(&block)
|
data/lib/sanford/host_data.rb
CHANGED
@@ -11,7 +11,7 @@ module Sanford
|
|
11
11
|
|
12
12
|
# NOTE: The `name` attribute shouldn't be removed, it is used to identify
|
13
13
|
# a `HostData`, particularly in error handlers
|
14
|
-
attr_reader :name, :logger, :verbose, :keep_alive, :runner, :
|
14
|
+
attr_reader :name, :logger, :verbose, :keep_alive, :runner, :error_procs
|
15
15
|
|
16
16
|
def initialize(service_host, options = nil)
|
17
17
|
service_host.configuration.init_proc.call
|
@@ -19,12 +19,12 @@ module Sanford
|
|
19
19
|
overrides = self.remove_nil_values(options || {})
|
20
20
|
configuration = service_host.configuration.to_hash.merge(overrides)
|
21
21
|
|
22
|
-
@name
|
23
|
-
@logger
|
24
|
-
@verbose
|
25
|
-
@keep_alive
|
26
|
-
@runner
|
27
|
-
@
|
22
|
+
@name = configuration[:name]
|
23
|
+
@logger = configuration[:logger]
|
24
|
+
@verbose = configuration[:verbose_logging]
|
25
|
+
@keep_alive = configuration[:receives_keep_alive]
|
26
|
+
@runner = configuration[:runner]
|
27
|
+
@error_procs = configuration[:error_procs]
|
28
28
|
|
29
29
|
@handlers = service_host.versioned_services.inject({}) do |hash, (version, services)|
|
30
30
|
hash.merge({ version => self.constantize_services(services) })
|
data/lib/sanford/version.rb
CHANGED
@@ -39,7 +39,7 @@ class Sanford::ErrorHandler
|
|
39
39
|
Sanford::Protocol::Response.new([ 567, 'custom message'], 'custom data')
|
40
40
|
end
|
41
41
|
host_data = Sanford::HostData.new(EmptyHost, @host_defaults.merge({
|
42
|
-
:
|
42
|
+
:error_procs => [ error_proc ]
|
43
43
|
}))
|
44
44
|
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
45
45
|
|
@@ -50,7 +50,7 @@ class Sanford::ErrorHandler
|
|
50
50
|
|
51
51
|
should "use an integer returned by the error proc to generate a protocol response" do
|
52
52
|
host_data = Sanford::HostData.new(EmptyHost, @host_defaults.merge({
|
53
|
-
:
|
53
|
+
:error_procs => [ proc{ 345 } ]
|
54
54
|
}))
|
55
55
|
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
56
56
|
|
@@ -61,7 +61,7 @@ class Sanford::ErrorHandler
|
|
61
61
|
|
62
62
|
should "use a symbol returned by the error proc to generate a protocol response" do
|
63
63
|
host_data = Sanford::HostData.new(EmptyHost, @host_defaults.merge({
|
64
|
-
:
|
64
|
+
:error_procs => [ proc{ :not_found } ]
|
65
65
|
}))
|
66
66
|
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
67
67
|
|
@@ -72,7 +72,7 @@ class Sanford::ErrorHandler
|
|
72
72
|
|
73
73
|
should "use the default behavior if the error proc doesn't return a valid response result" do
|
74
74
|
host_data = Sanford::HostData.new(EmptyHost, @host_defaults.merge({
|
75
|
-
:
|
75
|
+
:error_procs => [ proc{ true } ]
|
76
76
|
}))
|
77
77
|
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
78
78
|
|
@@ -83,7 +83,7 @@ class Sanford::ErrorHandler
|
|
83
83
|
should "use the default behavior for an exception raised by the error proc " \
|
84
84
|
"and ignore the original exception" do
|
85
85
|
host_data = Sanford::HostData.new(EmptyHost, @host_defaults.merge({
|
86
|
-
:
|
86
|
+
:error_procs => [ proc{ raise Sanford::NotFoundError } ]
|
87
87
|
}))
|
88
88
|
response = Sanford::ErrorHandler.new(@exception, host_data).run
|
89
89
|
|
@@ -130,4 +130,58 @@ class Sanford::ErrorHandler
|
|
130
130
|
|
131
131
|
end
|
132
132
|
|
133
|
+
class MultipleErrorProcsTest < ResponseFromProcTest
|
134
|
+
desc "with multiple error procs"
|
135
|
+
setup do
|
136
|
+
@first_called, @second_called, @third_called = nil, nil, nil
|
137
|
+
@host_data = Sanford::HostData.new(EmptyHost, @host_defaults.merge({
|
138
|
+
:error_procs => [ first_proc, second_proc, third_proc ]
|
139
|
+
}))
|
140
|
+
end
|
141
|
+
|
142
|
+
should "call every error proc" do
|
143
|
+
@error_handler = Sanford::ErrorHandler.new(RuntimeError.new('test'), @host_data)
|
144
|
+
@error_handler.run
|
145
|
+
|
146
|
+
assert_equal true, @first_called
|
147
|
+
assert_equal true, @second_called
|
148
|
+
assert_equal true, @third_called
|
149
|
+
end
|
150
|
+
|
151
|
+
should "should return the response of the last configured error proc " \
|
152
|
+
"that returned a valid response" do
|
153
|
+
@error_handler = Sanford::ErrorHandler.new(RuntimeError.new('test'), @host_data)
|
154
|
+
response = @error_handler.run
|
155
|
+
|
156
|
+
# use the second proc's generated response
|
157
|
+
assert_equal 987, response.code
|
158
|
+
|
159
|
+
exception = generate_exception(Sanford::NotFoundError, 'not found')
|
160
|
+
@error_handler = Sanford::ErrorHandler.new(exception, @host_data)
|
161
|
+
response = @error_handler.run
|
162
|
+
|
163
|
+
# use the third proc's generated response
|
164
|
+
assert_equal 876, response.code
|
165
|
+
end
|
166
|
+
|
167
|
+
def first_proc
|
168
|
+
proc{ @first_called = true }
|
169
|
+
end
|
170
|
+
|
171
|
+
def second_proc
|
172
|
+
proc do |exception, host_data, request|
|
173
|
+
@second_called = true
|
174
|
+
987
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def third_proc
|
179
|
+
proc do |exception, host_data, request|
|
180
|
+
@third_called = true
|
181
|
+
876 if exception.kind_of?(Sanford::NotFoundError)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
133
187
|
end
|
@@ -10,7 +10,7 @@ class Sanford::Host::Configuration
|
|
10
10
|
subject{ @configuration }
|
11
11
|
|
12
12
|
should have_instance_methods :name, :ip, :port, :pid_file, :logger,
|
13
|
-
:verbose_logging, :logger, :
|
13
|
+
:verbose_logging, :logger, :error_procs
|
14
14
|
|
15
15
|
should "default name to the class name of the host" do
|
16
16
|
assert_equal 'EmptyHost', subject.name
|
data/test/unit/host_data_test.rb
CHANGED
@@ -14,7 +14,7 @@ class Sanford::HostData
|
|
14
14
|
subject{ @host_data }
|
15
15
|
|
16
16
|
should have_instance_methods :name, :logger, :verbose, :keep_alive, :runner,
|
17
|
-
:
|
17
|
+
:error_procs, :run, :handler_class_for
|
18
18
|
|
19
19
|
should "default it's configuration from the service host, but allow overrides" do
|
20
20
|
host_data = Sanford::HostData.new(TestHost, :verbose_logging => false)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sanford
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 6
|
9
|
-
-
|
10
|
-
version: 0.6.
|
9
|
+
- 2
|
10
|
+
version: 0.6.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Collin Redding
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2013-
|
19
|
+
date: 2013-03-04 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
prerelease: false
|