Skip to content

Commit 115ba78

Browse files
authored
fix: log error message when exception is caught (#12770)
* fix: log error message when exception is caught * remove return since devrel tags are now moved inside the method to make the samples self runnable
1 parent f5e51eb commit 115ba78

File tree

14 files changed

+46
-36
lines changed

14 files changed

+46
-36
lines changed

managedkafka/snippets/clusters/create_cluster.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def create_cluster(
3333
memory_bytes: The memory to provision for the cluster in bytes.
3434
3535
Raises:
36-
This method will raise the exception if the operation errors or
36+
This method will raise the GoogleAPICallError exception if the operation errors or
3737
the timeout before the operation completes is reached.
3838
"""
3939
# [START managedkafka_create_cluster]
@@ -71,6 +71,7 @@ def create_cluster(
7171
# The duration of this operation can vary considerably, typically taking 10-40 minutes.
7272
# We can set a timeout of 3000s (50 minutes).
7373
operation = client.create_cluster(request=request, timeout=3000)
74+
print("Waiting for operation to finish...")
7475
response = operation.result()
7576
print("Created cluster:", response)
7677
except GoogleAPICallError:

managedkafka/snippets/clusters/delete_cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def delete_cluster(
2727
cluster_id: ID of the Kafka cluster.
2828
2929
Raises:
30-
This method will raise the exception if the operation errors or
30+
This method will raise the GoogleAPICallError exception if the operation errors or
3131
the timeout before the operation completes is reached.
3232
"""
3333
# [START managedkafka_delete_cluster]

managedkafka/snippets/clusters/get_cluster.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ def get_cluster(
2525
project_id: Google Cloud project ID.
2626
region: Cloud region.
2727
cluster_id: ID of the Kafka cluster.
28+
29+
Raises:
30+
This method will raise the NotFound exception if the cluster is not found.
2831
"""
2932
# [START managedkafka_get_cluster]
33+
from google.api_core.exceptions import NotFound
3034
from google.cloud import managedkafka_v1
3135

3236
# TODO(developer)
@@ -41,9 +45,10 @@ def get_cluster(
4145
name=cluster_path,
4246
)
4347

44-
cluster = client.get_cluster(request=request)
45-
print("Got cluster:", cluster)
46-
47-
return cluster
48+
try:
49+
cluster = client.get_cluster(request=request)
50+
print("Got cluster:", cluster)
51+
except NotFound as e:
52+
print(f"Failed to get cluster {cluster_id} with error: {e.message}")
4853

4954
# [END managedkafka_get_cluster]

managedkafka/snippets/clusters/list_clusters.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,4 @@ def list_clusters(
4141
for cluster in response:
4242
print("Got cluster:", cluster)
4343

44-
return [cluster.name for cluster in response]
45-
4644
# [END managedkafka_list_clusters]

managedkafka/snippets/clusters/update_cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def update_cluster(
2626
memory_bytes: The memory to provision for the cluster in bytes.
2727
2828
Raises:
29-
This method will raise the exception if the operation errors or
29+
This method will raise the GoogleAPICallError exception if the operation errors or
3030
the timeout before the operation completes is reached.
3131
"""
3232
# [START managedkafka_update_cluster]

managedkafka/snippets/consumergroups/delete_consumer_group.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def delete_consumer_group(
2929
consumer_group_id: ID of the Kafka consumer group.
3030
3131
Raises:
32-
This method will raise the exception if the consumer group is not found.
32+
This method will raise the NotFound exception if the consumer group or the parent resource is not found.
3333
"""
3434
# [START managedkafka_delete_consumergroup]
3535
from google.api_core.exceptions import NotFound
@@ -53,7 +53,7 @@ def delete_consumer_group(
5353
try:
5454
client.delete_consumer_group(request=request)
5555
print("Deleted consumer group")
56-
except NotFound:
57-
print(f"Consumer group {consumer_group_path} not found")
56+
except NotFound as e:
57+
print(f"Failed to delete consumer group {consumer_group_id} with error: {e.message}")
5858

5959
# [END managedkafka_delete_consumergroup]

managedkafka/snippets/consumergroups/get_consumer_group.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ def get_consumer_group(
2727
region: Cloud region.
2828
cluster_id: ID of the Kafka cluster.
2929
consumer_group_id: ID of the Kafka consumer group.
30+
31+
Raises:
32+
This method will raise the NotFound exception if the consumer group or the parent resource is not found.
3033
"""
3134
# [START managedkafka_get_consumergroup]
35+
from google.api_core.exceptions import NotFound
3236
from google.cloud import managedkafka_v1
3337

3438
# TODO(developer)
@@ -46,9 +50,10 @@ def get_consumer_group(
4650
name=consumer_group_path,
4751
)
4852

49-
consumer_group = client.get_consumer_group(request=request)
50-
print("Got consumer group:", consumer_group)
51-
52-
return consumer_group
53+
try:
54+
consumer_group = client.get_consumer_group(request=request)
55+
print("Got consumer group:", consumer_group)
56+
except NotFound as e:
57+
print(f"Failed to get consumer group {consumer_group_id} with error: {e.message}")
5358

5459
# [END managedkafka_get_consumergroup]

managedkafka/snippets/consumergroups/list_consumer_groups.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,4 @@ def list_consumer_groups(
4444
for consumer_group in response:
4545
print("Got consumer group:", consumer_group)
4646

47-
return [consumer_group.name for consumer_group in response]
48-
4947
# [END managedkafka_list_consumergroups]

managedkafka/snippets/consumergroups/update_consumer_group.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def update_consumer_group(
3333
partition_offsets: Configuration of the topic, represented as a map of partition indexes to their offset value.
3434
3535
Raises:
36-
This method will raise the exception if the consumer group is not found.
36+
This method will raise the NotFound exception if the consumer group or the parent resource is not found.
3737
"""
3838
# [START managedkafka_update_consumergroup]
3939
from google.api_core.exceptions import NotFound
@@ -74,7 +74,7 @@ def update_consumer_group(
7474
try:
7575
response = client.update_consumer_group(request=request)
7676
print("Updated consumer group:", response)
77-
except NotFound:
78-
print(f"Consumer group {consumer_group.name} not found")
77+
except NotFound as e:
78+
print(f"Failed to update consumer group {consumer_group_id} with error: {e.message}")
7979

8080
# [END managedkafka_update_consumergroup]

managedkafka/snippets/topics/create_topic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def create_topic(
3535
configs: Configuration of the topic.
3636
3737
Raises:
38-
This method will raise the exception if the topic already exists.
38+
This method will raise the AlreadyExists exception if the topic already exists.
3939
"""
4040
# [START managedkafka_create_topic]
4141
from google.api_core.exceptions import AlreadyExists
@@ -68,7 +68,7 @@ def create_topic(
6868
try:
6969
response = client.create_topic(request=request)
7070
print("Created topic:", response.name)
71-
except AlreadyExists:
72-
print(f"{topic.name} already exists")
71+
except AlreadyExists as e:
72+
print(f"Failed to create topic {topic.name} with error: {e.message}")
7373

7474
# [END managedkafka_create_topic]

0 commit comments

Comments
 (0)