def test_exceptions(): HubException() AuthenticationException() AuthorizationException(Response()) AuthorizationException(Response(noerror=True)) NotFoundException() BadRequestException(Response()) BadRequestException(Response(noerror=True)) OverLimitException() ServerException() BadGatewayException() GatewayTimeoutException() WaitTimeoutException() LockedException() HubDatasetNotFoundException("Hello") PermissionException("Hello") ShapeLengthException() ShapeArgumentNotFoundException() SchemaArgumentNotFoundException() ValueShapeError("Shape 1", "Shape 2") NoneValueException("Yahoo!") ModuleNotInstalledException("my_module") WrongUsernameException("usernameX") NotHubDatasetToOverwriteException() NotHubDatasetToAppendException() DynamicTensorNotFoundException() DynamicTensorShapeException("none") DynamicTensorShapeException("length") DynamicTensorShapeException("not_equal") DynamicTensorShapeException("another_cause")
def _get_max_shape(self, shape, max_shape): if max_shape is None: return tuple([s or self._int32max for s in shape]) elif isinstance(max_shape, int): assert max_shape == shape[0] return self._get_max_shape(shape, None) else: max_shape = tuple(max_shape) assert len(shape) == len(max_shape) for (s, ms) in zip(shape, max_shape): if not isinstance(ms, int): raise HubException("MaxShape Dimension should be int") if s is not None and s != ms: raise HubException( """Dimension in shape cannot be != max_shape dimension, if shape is not None """) assert s == ms or s is None and isinstance(ms, int) return max_shape
def get_proxy_command(proxy): ssh_proxy = "" if proxy and proxy != " " and proxy != "None" and proxy != "": if check_program_exists("ncat"): ssh_proxy = '-o "ProxyCommand=ncat --proxy-type socks5 --proxy {} %h %p"'.format( proxy) else: raise HubException( message= "This pod is behind the firewall. You need one more thing. Please install nmap by running `sudo apt-get install nmap` on Ubuntu or `brew install nmap` for Mac" ) return ssh_proxy
def check_token(self, access_token): auth = f"Bearer {access_token}" response = self.request("GET", config.CHECK_TOKEN_REST_SUFFIX, headers={"Authorization": auth}) try: response_dict = response.json() is_valid = response_dict["is_valid"] except Exception as e: logger.error(f"Exception occured while validating token: {e}.") raise HubException("Error while validating the token. \ Please try logging in using username ans password." ) return is_valid
def get_access_token(self, username, password): response = self.request( "GET", config.GET_TOKEN_REST_SUFFIX, json={ "username": username, "password": password }, ) try: token_dict = response.json() token = token_dict["token"] except Exception as e: logger.error(f"Exception occured while getting token: {e}.") raise HubException("Error while loggin in. \ Please try logging in using access token.") return token
def check_response_status(self, response): """ Check response status and throw corresponding exception on failure """ code = response.status_code if code < 200 or code >= 300: try: message = response.json()["description"] except Exception: message = " " logger.debug(f'Error received: status code: {code}, message: "{message}"') if code == 400: raise BadRequestException(response) elif response.status_code == 401: raise AuthenticationException() elif response.status_code == 403: raise AuthorizationException() elif response.status_code == 404: if message != " ": raise NotFoundException(message) else: raise NotFoundException elif response.status_code == 429: raise OverLimitException(message) elif response.status_code == 502: raise BadGatewayException() elif response.status_code == 504: raise GatewayTimeoutException(message) elif response.status_code == 423: raise LockedException(message) elif 500 <= response.status_code < 600: if "Server under maintenance" in response.content.decode(): raise ServerException( "Server under maintenance, please try again later." ) else: raise ServerException() else: msg = "An error occurred. Server response: {}".format( response.status_code ) raise HubException(message=msg)