Skip to content

Commit 5f0d69c

Browse files
Factored our error details processing; added to signal_with_start_workflow_execution
1 parent 937482e commit 5f0d69c

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

lib/temporal/connection/grpc.rb

+27-7
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ def start_workflow_execution(
118118

119119
client.start_workflow_execution(request)
120120
rescue ::GRPC::AlreadyExists => e
121-
cast_error = e.to_rpc_status&.details&.map do |any_error|
122-
next unless any_error.type_url == 'type.googleapis.com/temporal.api.errordetails.v1.WorkflowExecutionAlreadyStartedFailure'
123-
124-
any_error.unpack(Temporal::Api::ErrorDetails::V1::WorkflowExecutionAlreadyStartedFailure)
125-
end&.compact&.first
121+
error_details_from(e).each do |error|
122+
case error
123+
when Temporal::Api::ErrorDetails::V1::WorkflowExecutionAlreadyStartedFailure
124+
raise Temporal::WorkflowExecutionAlreadyStartedFailure.new(e.details, error.run_id)
125+
end
126+
end
126127

127-
raise Temporal::WorkflowExecutionAlreadyStartedFailure.new(e.details, cast_error&.run_id)
128+
raise Temporal::ApiError, e.details # unhandled error type
128129
end
129130

130131
SERVER_MAX_GET_WORKFLOW_EXECUTION_HISTORY_POLL = 30
@@ -138,7 +139,7 @@ def get_workflow_execution_history(
138139
event_type: :all,
139140
timeout: nil
140141
)
141-
if wait_for_new_event
142+
if wait_for_new_event
142143
if timeout.nil?
143144
# This is an internal error. Wrappers should enforce this.
144145
raise "You must specify a timeout when wait_for_new_event = true."
@@ -366,7 +367,17 @@ def signal_with_start_workflow_execution(
366367
request.workflow_id_reuse_policy = policy
367368
end
368369

370+
369371
client.signal_with_start_workflow_execution(request)
372+
rescue ::GRPC::AlreadyExists => e
373+
error_details_from(e).each do |error|
374+
case error
375+
when Temporal::Api::ErrorDetails::V1::WorkflowExecutionAlreadyStartedFailure
376+
raise Temporal::WorkflowExecutionAlreadyStartedFailure.new(e.details, error.run_id)
377+
end
378+
end
379+
380+
raise Temporal::ApiError, e.details # unhandled error type
370381
end
371382

372383
def reset_workflow_execution(namespace:, workflow_id:, run_id:, reason:, workflow_task_event_id:)
@@ -488,6 +499,15 @@ def client
488499
def can_poll?
489500
@poll
490501
end
502+
503+
def error_details_from(error)
504+
error.to_rpc_status&.details&.map do |any_error|
505+
type = Google::Protobuf::DescriptorPool.generated_pool.lookup any_error.type_url.split('/').last
506+
next if type.nil?
507+
508+
any_error.unpack type.msgclass
509+
end&.compact
510+
end
491511
end
492512
end
493513
end

spec/unit/lib/temporal/grpc_client_spec.rb

+22-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@
4949
Temporal::Api::WorkflowService::V1::SignalWithStartWorkflowExecutionResponse.new(run_id: 'xxx')
5050
end
5151

52-
before { allow(grpc_stub).to receive(:signal_with_start_workflow_execution).and_return(temporal_response) }
53-
5452
it 'starts a workflow with a signal with scalar arguments' do
53+
allow(grpc_stub).to receive(:signal_with_start_workflow_execution).and_return(temporal_response)
5554
subject.signal_with_start_workflow_execution(
5655
namespace: namespace,
5756
workflow_id: workflow_id,
@@ -79,6 +78,27 @@
7978
expect(request.signal_input.payloads[0].data).to eq('"what do you get if you multiply six by nine?"')
8079
end
8180
end
81+
82+
it 'provides the existing run_id when the workflow is already started' do
83+
allow(grpc_stub).to receive(:signal_with_start_workflow_execution).and_raise(already_started_error)
84+
85+
expect do
86+
subject.signal_with_start_workflow_execution(
87+
namespace: namespace,
88+
workflow_id: workflow_id,
89+
workflow_name: 'workflow_name',
90+
task_queue: 'task_queue',
91+
input: ['foo'],
92+
execution_timeout: 1,
93+
run_timeout: 2,
94+
task_timeout: 3,
95+
signal_name: 'the question',
96+
signal_input: 'what do you get if you multiply six by nine?',
97+
)
98+
end.to raise_error(Temporal::WorkflowExecutionAlreadyStartedFailure) do |e|
99+
expect(e.run_id).to eql(run_id)
100+
end
101+
end
82102
end
83103

84104
describe "#list_namespaces" do

0 commit comments

Comments
 (0)