circuitbox 0.10.1 → 0.10.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d3753a1304b550e062e4956cb9c4ddd366a1916
4
- data.tar.gz: 837d1d4e71f5ddaa3b9434d6333d846043d73303
3
+ metadata.gz: c55691f6ad22c899ad8425c2d412290ac47e0ede
4
+ data.tar.gz: c37774032e6e84cb0dd2c23ab9e87ef48ce22fe5
5
5
  SHA512:
6
- metadata.gz: e12e4b470624fc64ca42ada7b624c4233b0d8f80d6ac98d244833a0de261ef3d0e09b11205e6740a3a24514cccad03a959780dc9cae75f56a6ac83ee26435186
7
- data.tar.gz: 5a81d87b4a22644b5ea2bacf5a3ee2193ddbd896b15991f41ca9a21ae446fc74a844938cc9b761e2afe38b273cea4be184b84e43d892761d5c6d185e34478f58
6
+ metadata.gz: 00e370cf4194cafc3e9842c3077f1783e377466c2a2b186589ede61bc1f85faf9a47ee944ff8e29fa78db7c02760bb429a6f511301d24075fdcfcf195524f03e
7
+ data.tar.gz: 40a10dc8632d27a7da4b79a78288ff10494a90fd2a4b8db7aac8d41400e2b0d00e4042dab4564d49aeec48f8314b39f094ce0b783e00d4c4f2342bb761d9d35a
data/README.md CHANGED
@@ -169,11 +169,17 @@ c.use Circuitbox::FaradayMiddleware, exceptions: [Faraday::Error::TimeoutError]
169
169
  * `default_value` value to return for open circuits, defaults to 503 response
170
170
  wrapping the original response given by the service and stored as
171
171
  `original_response` property of the returned 503, this can be overwritten
172
- either with a static value or a `lambda` which is passed the
173
- original_response.
172
+ with either
173
+ * a static value
174
+ * a `lambda` which is passed the `original_response` and `original_error`.
175
+ `original_response` will be populated if Faraday returne an error response,
176
+ `original_error` will be populated if an error was thrown before Faraday
177
+ returned a response. (It will also accept a lambda with arity 1 that is
178
+ only passed `original_response`. This use is deprecated and will be removed
179
+ in the next major version.)
174
180
 
175
181
  ```ruby
176
- c.use Circuitbox::FaradayMiddleware, default_value: lambda { |response| ... }
182
+ c.use Circuitbox::FaradayMiddleware, default_value: lambda { |response, error| ... }
177
183
  ```
178
184
 
179
185
  * `identifier` circuit id, defaults to request url
@@ -207,6 +213,15 @@ c.use Circuitbox::FaradayMiddleware, open_circuit: lambda { |response| response.
207
213
 
208
214
  ### version next
209
215
 
216
+ ### v0.10.2
217
+ - Faraday middleware passes two arguments to the `default_value` callback, not
218
+ just one. First argument is still the error response from Faraday if there is
219
+ one. Second argument is the exception that caused the call to fail if it
220
+ failed before Faraday returned a response. Old behaviour is preserved if you
221
+ pass a lambda that takes just one argument, but this is deprecated and will be
222
+ removed in the next version of Circuitbox.
223
+ [dwaller](https://github.com/dwaller)
224
+
210
225
  ### v0.10.1
211
226
  - [Documentation fix](https://github.com/yammer/circuitbox/pull/29) [chiefcll](https://github.com/chiefcll)
212
227
  - [Faraday middleware fix](https://github.com/yammer/circuitbox/pull/30) [chiefcll](https://github.com/chiefcll)
@@ -11,9 +11,10 @@ class Circuitbox
11
11
  ]
12
12
 
13
13
  class NullResponse < Faraday::Response
14
- attr_reader :original_response
15
- def initialize(response = nil)
16
- @original_response = response
14
+ attr_reader :original_response, :original_exception
15
+ def initialize(response = nil, exception = nil)
16
+ @original_response = response
17
+ @original_exception = exception
17
18
  super(status: 503, response_headers: {})
18
19
  end
19
20
  end
@@ -35,8 +36,8 @@ class Circuitbox
35
36
  raise RequestFailed if open_circuit?(service_response)
36
37
  end
37
38
  end
38
- rescue Circuitbox::Error
39
- circuit_open_value(request_env, service_response)
39
+ rescue Circuitbox::Error => ex
40
+ circuit_open_value(request_env, service_response, ex)
40
41
  end
41
42
 
42
43
  def exceptions
@@ -66,13 +67,13 @@ class Circuitbox
66
67
  return @default_value if @default_value
67
68
 
68
69
  default = opts.fetch(:default_value) do
69
- lambda { |service_response| NullResponse.new(service_response) }
70
+ lambda { |service_response, exception| NullResponse.new(service_response, exception) }
70
71
  end
71
72
 
72
73
  @default_value = if default.respond_to?(:call)
73
74
  default
74
75
  else
75
- lambda { |_| default }
76
+ lambda { |*| default }
76
77
  end
77
78
  end
78
79
 
@@ -84,8 +85,16 @@ class Circuitbox
84
85
  @circuitbox ||= opts.fetch(:circuitbox, Circuitbox)
85
86
  end
86
87
 
87
- def circuit_open_value(env, service_response)
88
- env[:circuit_breaker_default_value] || default_value.call(service_response)
88
+ def circuit_open_value(env, service_response, exception)
89
+ env[:circuit_breaker_default_value] || call_default_lambda(service_response, exception)
90
+ end
91
+
92
+ def call_default_lambda(service_response, exception)
93
+ if default_value.arity == 2
94
+ default_value.call(service_response, exception)
95
+ else
96
+ default_value.call(service_response)
97
+ end
89
98
  end
90
99
 
91
100
  def circuit(env)
@@ -1,3 +1,3 @@
1
1
  class Circuitbox
2
- VERSION='0.10.1'
2
+ VERSION='0.10.2'
3
3
  end
@@ -27,13 +27,25 @@ class Circuitbox
27
27
  env = { url: "url" }
28
28
  give(circuitbox).circuit("url", anything) { circuit }
29
29
  give(circuit).run!(anything) { raise Circuitbox::Error }
30
- default_value_generator = lambda { |_| :sential }
30
+ default_value_generator = lambda { |response| :sential }
31
31
  middleware = FaradayMiddleware.new(app,
32
32
  circuitbox: circuitbox,
33
33
  default_value: default_value_generator)
34
34
  assert_equal :sential, middleware.call(env)
35
35
  end
36
36
 
37
+ def test_default_value_generator_lambda_passed_error
38
+ stub_circuitbox
39
+ env = { url: "url" }
40
+ give(circuitbox).circuit("url", anything) { circuit }
41
+ give(circuit).run!(anything) { raise Circuitbox::Error.new("error text") }
42
+ default_value_generator = lambda { |_,error| error.message }
43
+ middleware = FaradayMiddleware.new(app,
44
+ circuitbox: circuitbox,
45
+ default_value: default_value_generator)
46
+ assert_equal "error text", middleware.call(env)
47
+ end
48
+
37
49
  def test_overwrite_default_value_generator_static_value
38
50
  stub_circuitbox
39
51
  env = { url: "url" }
@@ -42,7 +42,8 @@ class Circuitbox
42
42
  open_circuit
43
43
  open_circuit_response = connection.get(failure_url)
44
44
  assert_equal open_circuit_response.status, 503
45
- assert open_circuit_response.original_response.nil?
45
+ assert_nil open_circuit_response.original_response
46
+ assert_kind_of Circuitbox::OpenCircuitError, open_circuit_response.original_exception
46
47
  end
47
48
 
48
49
  def test_closed_circuit_response
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circuitbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.10.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fahim Ferdous
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler