diff --git a/README.md b/README.md index 22049d4..4dd3af6 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,46 @@ or client = Client(token=os.getenv("DIGITALOCEAN_TOKEN"), retry_policy=MyRetryPolicy()) ``` +#### Error Handling + +When you make an API call that fails, `pydo` will raise an `HttpResponseError`. You can use a `try...except` block to catch this error and see what went wrong. + +Here's how you can handle errors and print the details: + +```python +from pydo import Client +from pydo.exceptions import HttpResponseError + +# Make sure to set your DIGITALOCEAN_TOKEN environment variable +client = Client(token=os.getenv("DIGITALOCEAN_TOKEN")) + +try: + # This line will cause an error because the droplet doesn't exist + client.droplets.get(droplet_id="non-existent-droplet") +except HttpResponseError as error: + print(f"Oops! An error occurred.") + print(f"The server responded with status code: {error.response.status_code}") + + # The error response from DigitalOcean contains more details in JSON format + error_details = error.response.json() + error_id = error_details.get('id') + error_message = error_details.get('message') + + if error_id and error_message: + print(f"DigitalOcean error ID: {error_id}") + print(f"Message: {error_message}") +``` + +When you run this code, you'll see output like this: + +``` +Oops! An error occurred. +The server responded with status code: 404 +DigitalOcean error ID: not_found +Message: The resource you were accessing could not be found. +``` +This makes it easy to understand why your API call failed. + # **Contributing** >Visit our [Contribuing Guide](CONTRIBUTING.md) for more information on getting diff --git a/src/pydo/_patch.py b/src/pydo/_patch.py index 350d70f..6629ecc 100644 --- a/src/pydo/_patch.py +++ b/src/pydo/_patch.py @@ -59,3 +59,4 @@ def patch_sdk(): you can't accomplish using the techniques described in https://aka.ms/azsdk/python/dpcodegen/python/customize """ + from pydo.operations import _patch # pylint: disable=unused-import, no-name-in-module diff --git a/src/pydo/aio/_patch.py b/src/pydo/aio/_patch.py index 37d16e1..121205d 100644 --- a/src/pydo/aio/_patch.py +++ b/src/pydo/aio/_patch.py @@ -64,3 +64,4 @@ def patch_sdk(): you can't accomplish using the techniques described in https://aka.ms/azsdk/python/dpcodegen/python/customize """ + from pydo.aio.operations import _patch # pylint: disable=unused-import, no-name-in-module diff --git a/src/pydo/aio/operations/_patch.py b/src/pydo/aio/operations/_patch.py index 39ea63f..2bd3e87 100644 --- a/src/pydo/aio/operations/_patch.py +++ b/src/pydo/aio/operations/_patch.py @@ -1,26 +1,28 @@ -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ -"""Customize generated code here. +"""This file is for patching generated code.""" +import functools +from typing import Any, Callable -Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize -""" -from typing import TYPE_CHECKING +from ._operations import RegistryOperations -if TYPE_CHECKING: - # pylint: disable=unused-import,ungrouped-imports - from typing import List -__all__ = ( - [] -) # type: List[str] # Add all objects you want publicly available to users at this package level +def patch_registry_delete_repository_manifest( + func: Callable[..., Any] +) -> Callable[..., Any]: + """This patch is to address a bug in the DO API. + The delete_repository_manifest endpoint does not support url encoding. + This patch will skip url encoding for the manifest_digest parameter. + """ -def patch_sdk(): - """Do not remove from this file. + @functools.wraps(func) + def wrapper( + self, manifest_digest: str, *args: Any, **kwargs: Any + ) -> Callable[..., Any]: + return func(self, manifest_digest, *args, **kwargs, _skip_url_encoding=True) - `patch_sdk` is a last resort escape hatch that allows you to do customizations - you can't accomplish using the techniques described in - https://aka.ms/azsdk/python/dpcodegen/python/customize - """ + return wrapper + + +RegistryOperations.delete_repository_manifest = patch_registry_delete_repository_manifest( + RegistryOperations.delete_repository_manifest +) \ No newline at end of file diff --git a/src/pydo/operations/_patch.py b/src/pydo/operations/_patch.py index 8a843f7..9d8c3f7 100644 --- a/src/pydo/operations/_patch.py +++ b/src/pydo/operations/_patch.py @@ -1,27 +1,29 @@ -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ -"""Customize generated code here. - -Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize +"""This file is for patching generated code. """ -from typing import TYPE_CHECKING +import functools +from typing import Any, Callable + +from ._operations import RegistryOperations -from ._operations import DropletsOperations as Droplets -if TYPE_CHECKING: - # pylint: disable=unused-import,ungrouped-imports - pass +def patch_registry_delete_repository_manifest( + func: Callable[..., Any] +) -> Callable[..., Any]: + """This patch is to address a bug in the DO API. + The delete_repository_manifest endpoint does not support url encoding. + This patch will skip url encoding for the manifest_digest parameter. + """ -__all__ = [] + @functools.wraps(func) + def wrapper( + self, manifest_digest: str, *args: Any, **kwargs: Any + ) -> Callable[..., Any]: + return func(self, manifest_digest, *args, **kwargs, _skip_url_encoding=True) + return wrapper -def patch_sdk(): - """Do not remove from this file. - `patch_sdk` is a last resort escape hatch that allows you to do customizations - you can't accomplish using the techniques described in - https://aka.ms/azsdk/python/dpcodegen/python/customize - """ +RegistryOperations.delete_repository_manifest = patch_registry_delete_repository_manifest( + RegistryOperations.delete_repository_manifest +) \ No newline at end of file