errors.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import logging
  3. from http import HTTPStatus
  4. from pathlib import Path
  5. from typing import Optional, Union
  6. import requests
  7. from requests.exceptions import HTTPError
  8. from modelscope.hub.constants import MODELSCOPE_REQUEST_ID
  9. from modelscope.utils.logger import get_logger
  10. logger = get_logger(log_level=logging.WARNING)
  11. class NotSupportError(Exception):
  12. pass
  13. class NoValidRevisionError(Exception):
  14. pass
  15. class NotExistError(Exception):
  16. pass
  17. class RequestError(Exception):
  18. pass
  19. class GitError(Exception):
  20. pass
  21. class InvalidParameter(Exception):
  22. pass
  23. class NotLoginException(Exception):
  24. pass
  25. class FileIntegrityError(Exception):
  26. pass
  27. class FileDownloadError(Exception):
  28. pass
  29. class CacheNotFound(Exception):
  30. """Exception thrown when the ModelScope cache is not found."""
  31. cache_dir: Union[str, Path]
  32. def __init__(self, msg: str, cache_dir: Union[str, Path], *args, **kwargs):
  33. super().__init__(msg, *args, **kwargs)
  34. self.cache_dir = cache_dir
  35. class CorruptedCacheException(Exception):
  36. """Exception for any unexpected structure in the ModelScope cache-system."""
  37. def get_request_id(response: requests.Response):
  38. if MODELSCOPE_REQUEST_ID in response.request.headers:
  39. return response.request.headers[MODELSCOPE_REQUEST_ID]
  40. else:
  41. return ''
  42. def is_ok(rsp):
  43. """ Check the request is ok
  44. Args:
  45. rsp (Response): The request response body
  46. Returns:
  47. bool: `True` if success otherwise `False`.
  48. """
  49. return rsp['Code'] == HTTPStatus.OK and rsp['Success']
  50. def _decode_response_error(response: requests.Response):
  51. if 'application/json' in response.headers.get('content-type', ''):
  52. message = response.json()
  53. else:
  54. message = response.content.decode('utf-8')
  55. return message
  56. def handle_http_post_error(response, url, request_body):
  57. try:
  58. response.raise_for_status()
  59. except HTTPError as error:
  60. message = _decode_response_error(response)
  61. raise HTTPError(
  62. 'Request %s with body: %s exception, '
  63. 'Response details: %s, request id: %s' %
  64. (url, request_body, message, get_request_id(response))) from error
  65. def handle_http_response(response: requests.Response,
  66. logger,
  67. cookies,
  68. model_id,
  69. raise_on_error: Optional[bool] = True) -> int:
  70. http_error_msg = ''
  71. if isinstance(response.reason, bytes):
  72. try:
  73. reason = response.reason.decode('utf-8')
  74. except UnicodeDecodeError:
  75. reason = response.reason.decode('iso-8859-1')
  76. else:
  77. reason = response.reason
  78. request_id = get_request_id(response)
  79. if 404 == response.status_code:
  80. http_error_msg = 'The request model: %s does not exist!' % (model_id)
  81. elif 403 == response.status_code:
  82. if cookies is None:
  83. http_error_msg = (
  84. f'Authentication token does not exist, failed to access model {model_id} '
  85. 'which may not exist or may be private. Please login first.')
  86. else:
  87. http_error_msg = f'The authentication token is invalid, failed to access model {model_id}.'
  88. elif 400 <= response.status_code < 500:
  89. http_error_msg = u'%s Client Error: %s, Request id: %s for url: %s' % (
  90. response.status_code, reason, request_id, response.url)
  91. elif 500 <= response.status_code < 600:
  92. http_error_msg = u'%s Server Error: %s, Request id: %s, for url: %s' % (
  93. response.status_code, reason, request_id, response.url)
  94. if http_error_msg and raise_on_error: # there is error.
  95. logger.error(http_error_msg)
  96. raise HTTPError(http_error_msg, response=response)
  97. else:
  98. return response.status_code
  99. def raise_on_error(rsp):
  100. """If response error, raise exception
  101. Args:
  102. rsp (_type_): The server response
  103. Raises:
  104. RequestError: the response error message.
  105. Returns:
  106. bool: True if request is OK, otherwise raise `RequestError` exception.
  107. """
  108. if rsp['Code'] == HTTPStatus.OK:
  109. return True
  110. else:
  111. raise RequestError(rsp['Message'])
  112. def datahub_raise_on_error(url, rsp, http_response: requests.Response):
  113. """If response error, raise exception
  114. Args:
  115. url (str): The request url
  116. rsp (HTTPResponse): The server response.
  117. http_response: the origin http response.
  118. Raises:
  119. RequestError: the http request error.
  120. Returns:
  121. bool: `True` if request is OK, otherwise raise `RequestError` exception.
  122. """
  123. if rsp.get('Code') == HTTPStatus.OK:
  124. return True
  125. else:
  126. request_id = rsp['RequestId']
  127. raise RequestError(
  128. f"Url = {url}, Request id={request_id} Code = {rsp['Code']} Message = {rsp['Message']},\
  129. Please specify correct dataset_name and namespace.")
  130. def raise_for_http_status(rsp):
  131. """Attempt to decode utf-8 first since some servers
  132. localize reason strings, for invalid utf-8, fall back
  133. to decoding with iso-8859-1.
  134. Args:
  135. rsp: The http response.
  136. Raises:
  137. HTTPError: The http error info.
  138. """
  139. http_error_msg = ''
  140. if isinstance(rsp.reason, bytes):
  141. try:
  142. reason = rsp.reason.decode('utf-8')
  143. except UnicodeDecodeError:
  144. reason = rsp.reason.decode('iso-8859-1')
  145. else:
  146. reason = rsp.reason
  147. request_id = get_request_id(rsp)
  148. if 404 == rsp.status_code:
  149. http_error_msg = 'The request resource(model or dataset) does not exist!,'
  150. 'url: %s, reason: %s' % (rsp.url, reason)
  151. elif 403 == rsp.status_code:
  152. http_error_msg = 'Authentication token does not exist or invalid.'
  153. elif 400 <= rsp.status_code < 500:
  154. http_error_msg = u'%s Client Error: %s, Request id: %s for url: %s' % (
  155. rsp.status_code, reason, request_id, rsp.url)
  156. elif 500 <= rsp.status_code < 600:
  157. http_error_msg = u'%s Server Error: %s, Request id: %s, for url: %s' % (
  158. rsp.status_code, reason, request_id, rsp.url)
  159. if http_error_msg:
  160. req = rsp.request
  161. if req.method == 'POST':
  162. http_error_msg = u'%s, body: %s' % (http_error_msg, req.body)
  163. raise HTTPError(http_error_msg, response=rsp)