Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 5116688

Browse files
author
Jon Yurek
committed
Ensure any result that's nil result becomes useful
Add a middleware that should be placed at the bottom of the Invocation stack. This middleware ensures that any time the invocation stack is called, there is a return value. If the stack comes back nil, then a message is put in its place implying an error. While this might not technically be the case that the service errored, a lack of return value should be considered an error and looked in to.
1 parent b23cfa8 commit 5116688

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

lib/cc/service/invocation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
require 'cc/service/invocation/with_retries'
33
require 'cc/service/invocation/with_metrics'
44
require 'cc/service/invocation/with_error_handling'
5+
require 'cc/service/invocation/with_return_values'
56

67
class CC::Service::Invocation
78
MIDDLEWARE = {
89
retries: WithRetries,
910
metrics: WithMetrics,
1011
error_handling: WithErrorHandling,
12+
return_values: WithReturnValues,
1113
}
1214

1315
attr_reader :result
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class CC::Service::Invocation
2+
class WithReturnValues
3+
def initialize(invocation, message = nil)
4+
@invocation = invocation
5+
@message = message || "An internal error happened"
6+
end
7+
8+
def call
9+
result = @invocation.call
10+
if result.nil?
11+
{ ok: false, message: @message }
12+
else
13+
result
14+
end
15+
end
16+
end
17+
end
18+

test/invocation_return_values_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require File.expand_path('../helper', __FILE__)
2+
3+
class InvocationReturnValuesTest < CC::Service::TestCase
4+
def test_success_returns_upstream_result
5+
handler = CC::Service::Invocation::WithReturnValues.new(
6+
lambda { :return_value },
7+
"error message"
8+
)
9+
10+
assert_equal :return_value, handler.call
11+
end
12+
13+
def test_empty_results_returns_hash
14+
handler = CC::Service::Invocation::WithReturnValues.new(
15+
lambda { nil },
16+
"error message"
17+
)
18+
19+
assert_equal( {ok: false, message: "error message"}, handler.call )
20+
end
21+
end

test/invocation_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ def test_success
1010
assert_equal :some_result, result
1111
end
1212

13+
def test_success_with_return_values
14+
service = FakeService.new(:some_result)
15+
16+
result = CC::Service::Invocation.invoke(service) do |i|
17+
i.with :return_values, "error"
18+
end
19+
20+
assert_equal 1, service.receive_count
21+
assert_equal :some_result, result
22+
end
23+
24+
def test_failure_with_return_values
25+
service = FakeService.new(nil)
26+
27+
result = CC::Service::Invocation.invoke(service) do |i|
28+
i.with :return_values, "error"
29+
end
30+
31+
assert_equal 1, service.receive_count
32+
assert_equal( {ok: false, message: "error"}, result )
33+
end
34+
1335
def test_retries
1436
service = FakeService.new
1537
service.fake_error = RuntimeError.new

0 commit comments

Comments
 (0)