Skip to content

Commit 7af99d0

Browse files
authored
Fix exception-swallowing code path (#623)
* Failing test * Fix exception-swallowing code path
1 parent 4e97841 commit 7af99d0

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

temporalio/client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,8 +5089,7 @@ async def start_workflow(
50895089
raise temporalio.exceptions.WorkflowAlreadyStartedError(
50905090
input.id, input.workflow, run_id=details.run_id
50915091
)
5092-
else:
5093-
raise
5092+
raise
50945093
handle: WorkflowHandle[Any, Any] = WorkflowHandle(
50955094
self._client,
50965095
req.workflow_id,

tests/test_client.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
import os
44
import uuid
55
from datetime import datetime, timedelta, timezone
6-
from typing import Any, List, Optional, Tuple, cast
6+
from typing import Any, List, Mapping, Optional, Tuple, cast
7+
from unittest import mock
78

9+
import google.protobuf.any_pb2
10+
import google.protobuf.message
811
import pytest
912
from google.protobuf import json_format
1013

14+
import temporalio.api.common.v1
1115
import temporalio.api.enums.v1
16+
import temporalio.api.errordetails.v1
17+
import temporalio.api.workflowservice.v1
1218
import temporalio.common
1319
import temporalio.exceptions
1420
from temporalio import workflow
@@ -80,6 +86,7 @@
8086
)
8187
from temporalio.converter import DataConverter
8288
from temporalio.exceptions import WorkflowAlreadyStartedError
89+
from temporalio.service import ServiceCall
8390
from temporalio.testing import WorkflowEnvironment
8491
from tests.helpers import (
8592
assert_eq_eventually,
@@ -283,6 +290,45 @@ async def test_terminate(client: Client, worker: ExternalWorker):
283290
assert list(err.value.cause.details) == ["arg1", "arg2"]
284291

285292

293+
async def test_rpc_already_exists_error_is_raised(client: Client):
294+
class start_workflow_execution(
295+
ServiceCall[
296+
temporalio.api.workflowservice.v1.StartWorkflowExecutionRequest,
297+
temporalio.api.workflowservice.v1.StartWorkflowExecutionResponse,
298+
]
299+
):
300+
already_exists_err = RPCError(
301+
"fake already exists error", RPCStatusCode.ALREADY_EXISTS, b""
302+
)
303+
already_exists_err._grpc_status = temporalio.api.common.v1.GrpcStatus(
304+
details=[
305+
google.protobuf.any_pb2.Any(
306+
type_url="not-WorkflowExecutionAlreadyStartedFailure", value=b""
307+
)
308+
],
309+
)
310+
311+
def __init__(self) -> None:
312+
pass
313+
314+
async def __call__(
315+
self,
316+
req: temporalio.api.workflowservice.v1.StartWorkflowExecutionRequest,
317+
*,
318+
retry: bool = False,
319+
metadata: Mapping[str, str] = {},
320+
timeout: Optional[timedelta] = None,
321+
) -> temporalio.api.workflowservice.v1.StartWorkflowExecutionResponse:
322+
raise self.already_exists_err
323+
324+
with mock.patch.object(
325+
client.workflow_service, "start_workflow_execution", start_workflow_execution()
326+
):
327+
with pytest.raises(RPCError) as err:
328+
await client.start_workflow("fake", id="fake", task_queue="fake")
329+
assert err.value.status == RPCStatusCode.ALREADY_EXISTS
330+
331+
286332
async def test_cancel_not_found(client: Client):
287333
with pytest.raises(RPCError) as err:
288334
await client.get_workflow_handle("does-not-exist").cancel()

0 commit comments

Comments
 (0)