Skip to content

Commit 003a287

Browse files
Ygnasopenshift-merge-bot[bot]
authored andcommitted
enhance error handling in _kube_api_error_handling
1 parent e7790c5 commit 003a287

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

src/codeflare_sdk/common/kubernetes_cluster/kube_api_helpers.py

+35-24
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,45 @@
1919

2020
import executing
2121
from kubernetes import client, config
22-
from urllib3.util import parse_url
23-
import os
22+
23+
ERROR_MESSAGES = {
24+
"Not Found": "The requested resource could not be located.\n"
25+
"Please verify the resource name and namespace.",
26+
"Unauthorized": "Access to the API is unauthorized.\n"
27+
"Check your credentials or permissions.",
28+
"Forbidden": "Access denied to the Kubernetes resource.\n"
29+
"Ensure your role has sufficient permissions for this operation.",
30+
"Conflict": "A conflict occurred with the RayCluster resource.\n"
31+
"Only one RayCluster with the same name is allowed. "
32+
"Please delete or rename the existing RayCluster before creating a new one with the desired name.",
33+
}
2434

2535

2636
# private methods
2737
def _kube_api_error_handling(
2838
e: Exception, print_error: bool = True
2939
): # pragma: no cover
30-
perm_msg = (
31-
"Action not permitted, have you put in correct/up-to-date auth credentials?"
32-
)
33-
nf_msg = "No instances found, nothing to be done."
34-
exists_msg = "Resource with this name already exists."
35-
if type(e) == config.ConfigException:
36-
raise PermissionError(perm_msg)
37-
if type(e) == executing.executing.NotOneValueFound:
40+
def print_message(message: str):
3841
if print_error:
39-
print(nf_msg)
40-
return
41-
if type(e) == client.ApiException:
42-
if e.reason == "Not Found":
43-
if print_error:
44-
print(nf_msg)
45-
return
46-
elif e.reason == "Unauthorized" or e.reason == "Forbidden":
47-
if print_error:
48-
print(perm_msg)
49-
return
50-
elif e.reason == "Conflict":
51-
raise FileExistsError(exists_msg)
52-
raise e
42+
print(message)
43+
44+
if isinstance(e, client.ApiException):
45+
# Retrieve message based on reason, defaulting if reason is not known
46+
message = ERROR_MESSAGES.get(
47+
e.reason, f"Unexpected API error encountered (Reason: {e.reason})"
48+
)
49+
full_message = f"{message}\nResponse: {e.body}"
50+
print_message(full_message)
51+
52+
elif isinstance(e, config.ConfigException):
53+
message = "Configuration error: Unable to load Kubernetes configuration. Verify the config file path and format."
54+
print_message(message)
55+
56+
elif isinstance(e, executing.executing.NotOneValueFound):
57+
message = "Execution error: Expected exactly one value in the operation but found none or multiple."
58+
print_message(message)
59+
60+
else:
61+
message = f"Unexpected error:\n{str(e)}"
62+
print_message(message)
63+
raise e

0 commit comments

Comments
 (0)