Skip to content

Commit 9686dee

Browse files
Improves error handling
Signed-off-by: Elena Kolevska <[email protected]>
1 parent 9bf0e27 commit 9686dee

File tree

4 files changed

+10
-30
lines changed

4 files changed

+10
-30
lines changed

dapr/aio/clients/grpc/client.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1741,7 +1741,7 @@ async def converse_alpha1(
17411741
ConversationResponse containing the conversation results
17421742
17431743
Raises:
1744-
DaprInternalError: If the Dapr runtime returns an error
1744+
DaprGrpcError: If the Dapr runtime returns an error
17451745
"""
17461746
inputs_pb = [
17471747
api_v1.ConversationInput(content=inp.content, role=inp.role, scrubPII=inp.scrub_pii)
@@ -1768,8 +1768,8 @@ async def converse_alpha1(
17681768

17691769
return ConversationResponse(context_id=response.contextID, outputs=outputs)
17701770

1771-
except Exception as e:
1772-
raise DaprInternalError(f'Error invoking conversation API: {e}')
1771+
except grpc.aio.AioRpcError as err:
1772+
raise DaprGrpcError(err) from err
17731773

17741774
async def wait(self, timeout_s: float):
17751775
"""Waits for sidecar to be available within the timeout.

dapr/clients/grpc/client.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,7 @@ def converse_alpha1(
17431743
ConversationResponse containing the conversation results
17441744
17451745
Raises:
1746-
DaprInternalError: If the Dapr runtime returns an error
1746+
DaprGrpcError: If the Dapr runtime returns an error
17471747
"""
17481748

17491749
inputs_pb = [
@@ -1770,9 +1770,8 @@ def converse_alpha1(
17701770
]
17711771

17721772
return ConversationResponse(context_id=response.contextID, outputs=outputs)
1773-
1774-
except Exception as e:
1775-
raise DaprInternalError(f'Error invoking conversation API: {e}')
1773+
except RpcError as err:
1774+
raise DaprGrpcError(err) from err
17761775

17771776
def wait(self, timeout_s: float):
17781777
"""Waits for sidecar to be available within the timeout.

tests/clients/test_dapr_grpc_client.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from google.rpc import status_pb2, code_pb2
2727

28-
from dapr.clients.exceptions import DaprGrpcError, DaprInternalError
28+
from dapr.clients.exceptions import DaprGrpcError
2929
from dapr.clients.grpc.client import DaprGrpcClient
3030
from dapr.clients import DaprClient
3131
from dapr.clients.grpc.subscription import StreamInactiveError
@@ -1229,19 +1229,10 @@ def test_converse_alpha1_error_handling(self):
12291229

12301230
inputs = [ConversationInput(content='Hello', role='user')]
12311231

1232-
with self.assertRaises(DaprInternalError) as context:
1232+
with self.assertRaises(DaprGrpcError) as context:
12331233
dapr.converse_alpha1(name='test-llm', inputs=inputs)
12341234
self.assertTrue('Invalid argument' in str(context.exception))
12351235

1236-
def test_converse_alpha1_empty_inputs(self):
1237-
dapr = DaprGrpcClient(f'{self.scheme}localhost:{self.grpc_port}')
1238-
1239-
# Test with empty inputs list
1240-
response = dapr.converse_alpha1(name='test-llm', inputs=[])
1241-
1242-
self.assertIsNotNone(response)
1243-
self.assertEqual(len(response.outputs), 0)
1244-
12451236

12461237
if __name__ == '__main__':
12471238
unittest.main()

tests/clients/test_dapr_grpc_client_async.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from dapr.aio.clients.grpc.client import DaprGrpcClientAsync
2525
from dapr.aio.clients import DaprClient
26-
from dapr.clients.exceptions import DaprGrpcError, DaprInternalError
26+
from dapr.clients.exceptions import DaprGrpcError
2727
from dapr.common.pubsub.subscription import StreamInactiveError
2828
from dapr.proto import common_v1
2929
from .fake_dapr_server import FakeDaprSidecar
@@ -1159,21 +1159,11 @@ async def test_converse_alpha1_error_handling(self):
11591159

11601160
inputs = [ConversationInput(content='Hello', role='user')]
11611161

1162-
with self.assertRaises(DaprInternalError) as context:
1162+
with self.assertRaises(DaprGrpcError) as context:
11631163
await dapr.converse_alpha1(name='test-llm', inputs=inputs)
11641164
self.assertTrue('Invalid argument' in str(context.exception))
11651165
await dapr.close()
11661166

1167-
async def test_converse_alpha1_empty_inputs(self):
1168-
dapr = DaprGrpcClientAsync(f'{self.scheme}localhost:{self.grpc_port}')
1169-
1170-
# Test with empty inputs list
1171-
response = await dapr.converse_alpha1(name='test-llm', inputs=[])
1172-
1173-
self.assertIsNotNone(response)
1174-
self.assertEqual(len(response.outputs), 0)
1175-
await dapr.close()
1176-
11771167

11781168
if __name__ == '__main__':
11791169
unittest.main()

0 commit comments

Comments
 (0)