From 5bdb8fb78b5cb0d01431891d8e55cb5510a4ece4 Mon Sep 17 00:00:00 2001 From: "mulin.lyh" Date: Thu, 13 Oct 2022 18:30:06 +0800 Subject: [PATCH] [to #45451935]fix: add create model detail log for create failed. Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10382795 --- modelscope/hub/api.py | 24 +++++++++++------------- modelscope/hub/errors.py | 17 +++++++++++++++-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/modelscope/hub/api.py b/modelscope/hub/api.py index 8dcfa5b0..214045dd 100644 --- a/modelscope/hub/api.py +++ b/modelscope/hub/api.py @@ -24,8 +24,8 @@ from modelscope.utils.constant import (DEFAULT_DATASET_REVISION, DownloadMode) from modelscope.utils.logger import get_logger from .errors import (InvalidParameter, NotExistError, RequestError, - datahub_raise_on_error, handle_http_response, is_ok, - raise_on_error) + datahub_raise_on_error, handle_http_post_error, + handle_http_response, is_ok, raise_on_error) from .utils.utils import (get_dataset_hub_endpoint, get_endpoint, model_id_to_group_owner_name) @@ -105,17 +105,15 @@ class HubApi: path = f'{self.endpoint}/api/v1/models' owner_or_group, name = model_id_to_group_owner_name(model_id) - r = requests.post( - path, - json={ - 'Path': owner_or_group, - 'Name': name, - 'ChineseName': chinese_name, - 'Visibility': visibility, # server check - 'License': license - }, - cookies=cookies) - r.raise_for_status() + body = { + 'Path': owner_or_group, + 'Name': name, + 'ChineseName': chinese_name, + 'Visibility': visibility, # server check + 'License': license + } + r = requests.post(path, json=body, cookies=cookies) + handle_http_post_error(r, path, body) raise_on_error(r.json()) model_repo_url = f'{get_endpoint()}/{model_id}' return model_repo_url diff --git a/modelscope/hub/errors.py b/modelscope/hub/errors.py index c095a6ec..fb483287 100644 --- a/modelscope/hub/errors.py +++ b/modelscope/hub/errors.py @@ -4,6 +4,10 @@ from http import HTTPStatus from requests.exceptions import HTTPError +from modelscope.utils.logger import get_logger + +logger = get_logger() + class NotExistError(Exception): pass @@ -45,15 +49,24 @@ def is_ok(rsp): return rsp['Code'] == HTTPStatus.OK and rsp['Success'] +def handle_http_post_error(response, url, request_body): + try: + response.raise_for_status() + except HTTPError as error: + logger.error('Request %s with body: %s exception, respoonse body: %s' % + (url, request_body, response.body)) + raise error + + def handle_http_response(response, logger, cookies, model_id): try: response.raise_for_status() - except HTTPError: + except HTTPError as error: if cookies is None: # code in [403] and logger.error( f'Authentication token does not exist, failed to access model {model_id} which may not exist or may be \ private. Please login first.') - raise + raise error def raise_on_error(rsp):