-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an error-handling example and client docs
Signed-off-by: Elena Kolevska <[email protected]>
- Loading branch information
1 parent
7284b9b
commit 1127b1b
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Example - Error handling | ||
|
||
This guide demonstrates handling `DaprGrpcError` errors when using the Dapr python-SDK. It's important to note that not all Dapr gRPC status errors are currently captured and transformed into a `DaprGrpcError` by the SDK. Efforts are ongoing to enhance this aspect, and contributions are welcome. For detailed information on error handling in Dapr, refer to the [official documentation](https://docs.dapr.io/reference/errors/). | ||
|
||
The example involves creating a DaprClient and invoking the save_state method. | ||
It uses the default configuration from Dapr init in [self-hosted mode](https://github.com/dapr/cli#install-dapr-on-your-local-machine-self-hosted). | ||
|
||
## Pre-requisites | ||
|
||
- [Dapr CLI and initialized environment](https://docs.dapr.io/getting-started) | ||
- [Install Python 3.8+](https://www.python.org/downloads/) | ||
|
||
## Install Dapr python-SDK | ||
|
||
<!-- Our CI/CD pipeline automatically installs the correct version, so we can skip this step in the automation --> | ||
|
||
```bash | ||
pip3 install dapr dapr-ext-grpc | ||
``` | ||
|
||
## Run the example | ||
|
||
To run this example, the following code can be used: | ||
|
||
<!-- STEP | ||
name: Run error handling example | ||
expected_stdout_lines: | ||
- "== APP == Status code: StatusCode.INVALID_ARGUMENT" | ||
- "== APP == Message: input key/keyPrefix 'key||' can't contain '||'" | ||
- "== APP == Error code: DAPR_STATE_ILLEGAL_KEY" | ||
- "== APP == Error info(reason): DAPR_STATE_ILLEGAL_KEY" | ||
- "== APP == Resource info (resource type): state" | ||
- "== APP == Resource info (resource name): statestore" | ||
- "== APP == Bad request (field): key||" | ||
- "== APP == Bad request (description): input key/keyPrefix 'key||' can't contain '||'" | ||
timeout_seconds: 5 | ||
--> | ||
|
||
```bash | ||
dapr run -- python3 error_handling.py | ||
``` | ||
<!-- END_STEP --> | ||
|
||
The output should be as follows: | ||
|
||
``` | ||
== APP == Status code: StatusCode.INVALID_ARGUMENT | ||
== APP == Message: input key/keyPrefix 'key||' can't contain '||' | ||
== APP == Error code: DAPR_STATE_ILLEGAL_KEY | ||
== APP == Error info(reason): DAPR_STATE_ILLEGAL_KEY | ||
== APP == Resource info (resource type): state | ||
== APP == Resource info (resource name): statestore | ||
== APP == Bad request (field): key|| | ||
== APP == Bad request (description): input key/keyPrefix 'key||' can't contain '||' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
from dapr.clients import DaprClient | ||
from dapr.clients.exceptions import DaprGrpcError | ||
|
||
with DaprClient() as d: | ||
storeName = 'statestore' | ||
|
||
key = 'key||' | ||
value = 'value_1' | ||
|
||
# Wait for sidecar to be up within 5 seconds. | ||
d.wait(5) | ||
|
||
# Save single state. | ||
try: | ||
d.save_state(store_name=storeName, key=key, value=value) | ||
except DaprGrpcError as err: | ||
print(f'Status code: {err.code()}') | ||
print(f"Message: {err.message()}") | ||
print(f"Error code: {err.error_code()}") | ||
print(f"Error info(reason): {err.error_info.reason}") | ||
print(f"Resource info (resource type): {err.resource_info.resource_type}") | ||
print(f"Resource info (resource name): {err.resource_info.resource_name}") | ||
print(f"Bad request (field): {err.bad_request.field_violations[0].field}") | ||
print(f"Bad request (description): {err.bad_request.field_violations[0].description}") |