|
19 | 19 |
|
20 | 20 | import executing
|
21 | 21 | 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 | +} |
24 | 34 |
|
25 | 35 |
|
26 | 36 | # private methods
|
27 | 37 | def _kube_api_error_handling(
|
28 | 38 | e: Exception, print_error: bool = True
|
29 | 39 | ): # 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): |
38 | 41 | 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