-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improves error handling for grpc Status messages Signed-off-by: Elena Kolevska <[email protected]> * Missed a line Signed-off-by: Elena Kolevska <[email protected]> * Adds deps Signed-off-by: Elena Kolevska <[email protected]> * Add type ignores Signed-off-by: Elena Kolevska <[email protected]> * Handles rich errors for get and delete state Signed-off-by: Elena Kolevska <[email protected]> * Update all tests for state endpoints Signed-off-by: Elena Kolevska <[email protected]> * Adds error handling for pubsub publishing Signed-off-by: Elena Kolevska <[email protected]> * Tests cover all status details Signed-off-by: Elena Kolevska <[email protected]> * Linter fixes Signed-off-by: Elena Kolevska <[email protected]> * Adds an error-handling example and client docs Signed-off-by: Elena Kolevska <[email protected]> * Runs ruff Signed-off-by: Elena Kolevska <[email protected]> * Adds json() Signed-off-by: Elena Kolevska <[email protected]> * Removes unneeded import Signed-off-by: Elena Kolevska <[email protected]> * Type fix Signed-off-by: Elena Kolevska <[email protected]> * linter Signed-off-by: Elena Kolevska <[email protected]> * Apply suggestions from code review Signed-off-by: Bernd Verst <[email protected]> * example fix Signed-off-by: Elena Kolevska <[email protected]> * Removing link which does not exist yet Signed-off-by: Bernd Verst <[email protected]> --------- Signed-off-by: Elena Kolevska <[email protected]> Signed-off-by: Bernd Verst <[email protected]> Co-authored-by: Bernd Verst <[email protected]>
- Loading branch information
1 parent
ef73209
commit fc0e9d1
Showing
10 changed files
with
586 additions
and
24 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
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,57 @@ | ||
# 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/). | ||
|
||
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 '||'" | ||
- "== APP == JSON: {\"status_code\": \"INVALID_ARGUMENT\", \"message\": \"input key/keyPrefix 'key||' can't contain '||'\", \"error_code\": \"DAPR_STATE_ILLEGAL_KEY\", \"details\": {\"error_info\": {\"@type\": \"type.googleapis.com/google.rpc.ErrorInfo\", \"reason\": \"DAPR_STATE_ILLEGAL_KEY\", \"domain\": \"dapr.io\"}, \"retry_info\": null, \"debug_info\": null, \"quota_failure\": null, \"precondition_failure\": null, \"bad_request\": {\"@type\": \"type.googleapis.com/google.rpc.BadRequest\", \"field_violations\": [{\"field\": \"key||\", \"description\": \"input key/keyPrefix 'key||' can't contain '||'\"}]}, \"request_info\": null, \"resource_info\": {\"@type\": \"type.googleapis.com/google.rpc.ResourceInfo\", \"resource_type\": \"state\", \"resource_name\": \"statestore\"}, \"help\": null, \"localized_message\": null}}" | ||
timeout_seconds: 5 | ||
--> | ||
|
||
```bash | ||
dapr run -- python3 error_handling.py | ||
``` | ||
<!-- END_STEP --> | ||
|
||
The output should be as follows: | ||
|
||
``` | ||
== APP == Status code: 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 '||' | ||
== APP == JSON: {"status_code": "INVALID_ARGUMENT", "message": "input key/keyPrefix 'key||' can't contain '||'", "error_code": "DAPR_STATE_ILLEGAL_KEY", "details": {"error_info": {"@type": "type.googleapis.com/google.rpc.ErrorInfo", "reason": "DAPR_STATE_ILLEGAL_KEY", "domain": "dapr.io"}, "retry_info": null, "debug_info": null, "quota_failure": null, "precondition_failure": null, "bad_request": {"@type": "type.googleapis.com/google.rpc.BadRequest", "field_violations": [{"field": "key||", "description": "input key/keyPrefix 'key||' can't contain '||'"}]}, "request_info": null, "resource_info": {"@type": "type.googleapis.com/google.rpc.ResourceInfo", "resource_type": "state", "resource_name": "statestore"}, "help": null, "localized_message": null}} | ||
``` |
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,41 @@ | ||
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()}', flush=True) | ||
print(f'Message: {err.details()}', flush=True) | ||
print(f'Error code: {err.error_code()}', flush=True) | ||
|
||
if err.status_details().error_info is not None: | ||
print(f'Error info(reason): {err.status_details().error_info["reason"]}', flush=True) | ||
if err.status_details().resource_info is not None: | ||
print( | ||
f'Resource info (resource type): {err.status_details().resource_info["resource_type"]}', | ||
flush=True, | ||
) | ||
print( | ||
f'Resource info (resource name): {err.status_details().resource_info["resource_name"]}', | ||
flush=True, | ||
) | ||
if err.status_details().bad_request is not None: | ||
print( | ||
f'Bad request (field): {err.status_details().bad_request["field_violations"][0]["field"]}', | ||
flush=True, | ||
) | ||
print( | ||
f'Bad request (description): {err.status_details().bad_request["field_violations"][0]["description"]}', | ||
flush=True, | ||
) | ||
print(f'JSON: {err.json()}', flush=True) |
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
Oops, something went wrong.