Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/pydo/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/pydo/aio/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 22 additions & 20 deletions src/pydo/aio/operations/_patch.py
Original file line number Diff line number Diff line change
@@ -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
)
40 changes: 21 additions & 19 deletions src/pydo/operations/_patch.py
Original file line number Diff line number Diff line change
@@ -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
)