# Path: valohai_cli/exceptions.py
# class APIError(CLIException):
#     kind = 'API Error'
# 
#     def __init__(self, response: Response) -> None:
#         if '<!DOCTYPE html>' in response.text:
#             # Don't shower the user with a blob of HTML
#             text = 'Internal error'
#         else:
#             text = response.text
#         super().__init__(text)
#         self.response = response
#         self.request = response.request
# 
#     @property
#     def error_json(self) -> Union[None, dict, list]:
#         try:
#             error_json = self.response.json()
#             if isinstance(error_json, (dict, list)):
#                 return error_json
#         except Exception:
#             pass
#         return None
# 
#     @property
#     def code(self) -> Optional[Any]:
#         """
#         Attempt to retrieve a top-level error code from the response.
#         """
#         error_json = self.error_json
#         if isinstance(error_json, dict):
#             return error_json.get('code')
#         return None
# 
#     def format_message(self) -> str:
#         try:
#             error_json = self.error_json
#             if error_json:
#                 from .utils.error_fmt import format_error_data
#                 try:
#                     return format_error_data(error_json)
#                 except Exception:
#                     return pformat(error_json, indent=2)  # This should be more readable than raw JSON
#         except Exception:
#             pass
#         return super().format_message()
# 
# Path: valohai_cli/utils/api_error_utils.py
# def find_error(
#     response_data: Any,
#     *,
#     code: Optional[StringOrPattern] = None,
#     message: Optional[StringOrPattern] = None,
#     matcher: Optional[Callable] = None,
# ) -> Optional[Any]:
#     iterable: Optional[Iterable[Any]] = None
#     if isinstance(response_data, str):
#         return match_error({'message': response_data, 'code': None}, code=code, message=message, matcher=matcher)
# 
#     if isinstance(response_data, dict):
#         if 'code' in response_data and 'message' in response_data:  # Smells like the actual error object
#             return match_error(response_data, code=code, message=message, matcher=matcher)
#         iterable = response_data.values()
#     elif isinstance(response_data, list):
#         iterable = response_data
# 
#     if iterable is not None:
#         for value in iterable:
#             rv = find_error(value, code=code, message=message, matcher=matcher)
#             if rv:
#                 return rv
#     else:
#         raise TypeError(f'Can not find_error in {response_data!r}')
#     return None


from typing import Iterable
from valohai_cli.exceptions import APIError
from valohai_cli.utils.api_error_utils import find_error
import click




class ExecutionCreationAPIError(APIError):
    def get_hints(self) -> Iterable[str]:
        try:
            error_json = self.response.json()
        except Exception:
            return

        if not isinstance(error_json, dict):
            return

        if find_error(error_json.get('environment'), code='does_not_exist'):
            yield 'Run `{}` to see the complete list of available environments.'.format(
                click.style('vh environments', bold=True),

# Path: valohai_cli/exceptions.py
# class APIError(CLIException):
#     kind = 'API Error'
# 
#     def __init__(self, response: Response) -> None:
#         if '<!DOCTYPE html>' in response.text:
#             # Don't shower the user with a blob of HTML
#             text = 'Internal error'
#         else:
#             text = response.text
#         super().__init__(text)
#         self.response = response
#         self.request = response.request
# 
#     @property
#     def error_json(self) -> Union[None, dict, list]:
#         try:
#             error_json = self.response.json()
#             if isinstance(error_json, (dict, list)):
#                 return error_json
#         except Exception:
#             pass
#         return None
# 
#     @property
#     def code(self) -> Optional[Any]:
#         """
#         Attempt to retrieve a top-level error code from the response.
#         """
#         error_json = self.error_json
#         if isinstance(error_json, dict):
#             return error_json.get('code')
#         return None
# 
#     def format_message(self) -> str:
#         try:
#             error_json = self.error_json
#             if error_json:
#                 from .utils.error_fmt import format_error_data
#                 try:
#                     return format_error_data(error_json)
#                 except Exception:
#                     return pformat(error_json, indent=2)  # This should be more readable than raw JSON
#         except Exception:
#             pass
#         return super().format_message()
# 
# Path: valohai_cli/utils/api_error_utils.py
# def find_error(
#     response_data: Any,
#     *,
#     code: Optional[StringOrPattern] = None,
#     message: Optional[StringOrPattern] = None,
#     matcher: Optional[Callable] = None,
# ) -> Optional[Any]:
#     iterable: Optional[Iterable[Any]] = None
#     if isinstance(response_data, str):
#         return match_error({'message': response_data, 'code': None}, code=code, message=message, matcher=matcher)
# 
#     if isinstance(response_data, dict):
#         if 'code' in response_data and 'message' in response_data:  # Smells like the actual error object
#             return match_error(response_data, code=code, message=message, matcher=matcher)
#         iterable = response_data.values()
#     elif isinstance(response_data, list):
#         iterable = response_data
# 
#     if iterable is not None:
#         for value in iterable:
#             rv = find_error(value, code=code, message=message, matcher=matcher)
#             if rv:
#                 return rv
#     else:
#         raise TypeError(f'Can not find_error in {response_data!r}')
#     return None


# Path: valohai_cli/commands/execution/run/excs.py
from typing import Iterable
from valohai_cli.exceptions import APIError
from valohai_cli.utils.api_error_utils import find_error
import click





class ExecutionCreationAPIError(APIError):
    def get_hints(self) -> Iterable[str]:
        try:
            error_json = self.response.json()
        except Exception:
            return

        if not isinstance(error_json, dict):
            return

        if find_error(error_json.get('environment'), code='does_not_exist'):
            yield 'Run `{}` to see the complete list of available environments.'.format(
                click.style('vh environments', bold=True),

            )