def _handle_api_error(error: ApiException, raise_client_exception: bool = False) -> Any: """ Handle a ApiException from the Kubernetes client Args: error: ApiException to handle raise_client_exception: Should the method raise an error on client errors Returns: The the stringified version of the errors JSON body Raises: ApiException: If the ``raise_client_exception`` argument is set to ``True`` """ error_body = loads_json(error.body) if not error_body: error_body = {"message": "An unknown error occurred"} if Kubernetes._is_client_error(error.status): reason = camel_case_split(str(error_body.get("reason", "Unknown"))) logger.info( title=f"{cf.yellow}{cf.bold}{reason}{cf.reset}", message= f" ({error.status} {cf.italic}{error_body['message'].capitalize()}{cf.reset})", ) if raise_client_exception: raise error else: logger.error(error=error, raise_exception=True) return error_body
def test_loads_json_array_exception() -> None: with pytest.raises(TypeError): loads_json('["a", "b"]')
def test_loads_json(value: str, expected_value: Dict[str, Any]) -> None: assert loads_json(value) == expected_value