errors.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. """Contains all custom errors."""
  2. from pathlib import Path
  3. from httpx import HTTPError, Response
  4. # CACHE ERRORS
  5. class CacheNotFound(Exception):
  6. """Exception thrown when the Huggingface cache is not found."""
  7. cache_dir: str | Path
  8. def __init__(self, msg: str, cache_dir: str | Path, *args, **kwargs):
  9. super().__init__(msg, *args, **kwargs)
  10. self.cache_dir = cache_dir
  11. class CorruptedCacheException(Exception):
  12. """Exception for any unexpected structure in the Huggingface cache-system."""
  13. # HEADERS ERRORS
  14. class LocalTokenNotFoundError(EnvironmentError):
  15. """Raised if local token is required but not found."""
  16. # HTTP ERRORS
  17. class OfflineModeIsEnabled(ConnectionError):
  18. """Raised when a request is made but `HF_HUB_OFFLINE=1` is set as environment variable."""
  19. class HfHubHTTPError(HTTPError, OSError):
  20. """
  21. HTTPError to inherit from for any custom HTTP Error raised in HF Hub.
  22. Any HTTPError is converted at least into a `HfHubHTTPError`. If some information is
  23. sent back by the server, it will be added to the error message.
  24. Added details:
  25. - Request ID sourced from headers in order of precedence: "X-Request-Id", "X-Amzn-Trace-Id", "X-Amz-Cf-Id".
  26. - Server error message from the header "X-Error-Message".
  27. - Server error message if we can found one in the response body.
  28. Example:
  29. ```py
  30. import httpx
  31. from huggingface_hub.utils import get_session, hf_raise_for_status, HfHubHTTPError
  32. response = get_session().post(...)
  33. try:
  34. hf_raise_for_status(response)
  35. except HfHubHTTPError as e:
  36. print(str(e)) # formatted message
  37. e.request_id, e.server_message # details returned by server
  38. # Complete the error message with additional information once it's raised
  39. e.append_to_message("\n`create_commit` expects the repository to exist.")
  40. raise
  41. ```
  42. """
  43. def __init__(
  44. self,
  45. message: str,
  46. *,
  47. response: Response,
  48. server_message: str | None = None,
  49. ):
  50. self.request_id = (
  51. response.headers.get("x-request-id")
  52. or response.headers.get("X-Amzn-Trace-Id")
  53. or response.headers.get("x-amz-cf-id")
  54. )
  55. self.server_message = server_message
  56. self.response = response
  57. self.request = response.request
  58. super().__init__(message)
  59. def append_to_message(self, additional_message: str) -> None:
  60. """Append additional information to the `HfHubHTTPError` initial message."""
  61. self.args = (self.args[0] + additional_message,) + self.args[1:]
  62. @classmethod
  63. def _reconstruct_hf_hub_http_error(
  64. cls, message: str, response: Response, server_message: str | None
  65. ) -> "HfHubHTTPError":
  66. return cls(message, response=response, server_message=server_message)
  67. def __reduce_ex__(self, protocol):
  68. """Fix pickling of Exception subclass with kwargs. We need to override __reduce_ex__ of the parent class"""
  69. return (self.__class__._reconstruct_hf_hub_http_error, (str(self), self.response, self.server_message))
  70. # INFERENCE CLIENT ERRORS
  71. class InferenceTimeoutError(HTTPError, TimeoutError):
  72. """Error raised when a model is unavailable or the request times out."""
  73. # INFERENCE ENDPOINT ERRORS
  74. class InferenceEndpointError(Exception):
  75. """Generic exception when dealing with Inference Endpoints."""
  76. class InferenceEndpointTimeoutError(InferenceEndpointError, TimeoutError):
  77. """Exception for timeouts while waiting for Inference Endpoint."""
  78. # SAFETENSORS ERRORS
  79. class SafetensorsParsingError(Exception):
  80. """Raised when failing to parse a safetensors file metadata.
  81. This can be the case if the file is not a safetensors file or does not respect the specification.
  82. """
  83. class NotASafetensorsRepoError(Exception):
  84. """Raised when a repo is not a Safetensors repo i.e. doesn't have either a `model.safetensors` or a
  85. `model.safetensors.index.json` file.
  86. """
  87. # TEXT GENERATION ERRORS
  88. class TextGenerationError(HTTPError):
  89. """Generic error raised if text-generation went wrong."""
  90. # Text Generation Inference Errors
  91. class ValidationError(TextGenerationError):
  92. """Server-side validation error."""
  93. class GenerationError(TextGenerationError):
  94. pass
  95. class OverloadedError(TextGenerationError):
  96. pass
  97. class IncompleteGenerationError(TextGenerationError):
  98. pass
  99. class UnknownError(TextGenerationError):
  100. pass
  101. # VALIDATION ERRORS
  102. class HFValidationError(ValueError):
  103. """Generic exception thrown by `huggingface_hub` validators.
  104. Inherits from [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError).
  105. """
  106. # FILE METADATA ERRORS
  107. class DryRunError(OSError):
  108. """Error triggered when a dry run is requested but cannot be performed (e.g. invalid repo)."""
  109. class FileMetadataError(OSError):
  110. """Error triggered when the metadata of a file on the Hub cannot be retrieved (missing ETag or commit_hash).
  111. Inherits from `OSError` for backward compatibility.
  112. """
  113. # BUCKET ERRORS
  114. class BucketNotFoundError(HfHubHTTPError):
  115. """
  116. Raised when trying to access a bucket that does not exist.
  117. Attributes:
  118. bucket_id (`str` or `None`):
  119. The bucket id (namespace/name) that was not found, if it could be determined from the request URL.
  120. Example:
  121. ```py
  122. >>> from huggingface_hub import bucket_info
  123. >>> bucket_info("<non_existent_bucket>")
  124. (...)
  125. huggingface_hub.errors.BucketNotFoundError: 404 Client Error. (Request ID: XXX)
  126. Bucket Not Found for url: https://huggingface.co/api/buckets/namespace/name.
  127. Please make sure you specified the correct bucket id (namespace/name).
  128. If the bucket is private, make sure you are authenticated and your token has the required permissions.
  129. ```
  130. """
  131. bucket_id: str | None = None
  132. # REPOSITORY ERRORS
  133. class RepositoryNotFoundError(HfHubHTTPError):
  134. """
  135. Raised when trying to access a hf.co URL with an invalid repository name, or
  136. with a private repo name the user does not have access to.
  137. Attributes:
  138. repo_id (`str` or `None`):
  139. The repo id that was not found, if it could be determined from the request URL.
  140. repo_type (`str` or `None`):
  141. The repo type ("model", "dataset", or "space"), if it could be determined from the request URL.
  142. Example:
  143. ```py
  144. >>> from huggingface_hub import model_info
  145. >>> model_info("<non_existent_repository>")
  146. (...)
  147. huggingface_hub.errors.RepositoryNotFoundError: 401 Client Error. (Request ID: PvMw_VjBMjVdMz53WKIzP)
  148. Repository Not Found for url: https://huggingface.co/api/models/%3Cnon_existent_repository%3E.
  149. Please make sure you specified the correct `repo_id` and `repo_type`.
  150. If the repo is private, make sure you are authenticated and your token has the required permissions.
  151. Invalid username or password.
  152. ```
  153. """
  154. repo_id: str | None = None
  155. repo_type: str | None = None
  156. class GatedRepoError(RepositoryNotFoundError):
  157. """
  158. Raised when trying to access a gated repository for which the user is not on the
  159. authorized list.
  160. Note: derives from `RepositoryNotFoundError` to ensure backward compatibility.
  161. Example:
  162. ```py
  163. >>> from huggingface_hub import model_info
  164. >>> model_info("<gated_repository>")
  165. (...)
  166. huggingface_hub.errors.GatedRepoError: 403 Client Error. (Request ID: ViT1Bf7O_026LGSQuVqfa)
  167. Cannot access gated repo for url https://huggingface.co/api/models/ardent-figment/gated-model.
  168. Access to model ardent-figment/gated-model is restricted and you are not in the authorized list.
  169. Visit https://huggingface.co/ardent-figment/gated-model to ask for access.
  170. ```
  171. """
  172. class DisabledRepoError(HfHubHTTPError):
  173. """
  174. Raised when trying to access a repository that has been disabled by its author.
  175. Example:
  176. ```py
  177. >>> from huggingface_hub import dataset_info
  178. >>> dataset_info("laion/laion-art")
  179. (...)
  180. huggingface_hub.errors.DisabledRepoError: 403 Client Error. (Request ID: Root=1-659fc3fa-3031673e0f92c71a2260dbe2;bc6f4dfb-b30a-4862-af0a-5cfe827610d8)
  181. Cannot access repository for url https://huggingface.co/api/datasets/laion/laion-art.
  182. Access to this resource is disabled.
  183. ```
  184. """
  185. # REVISION ERROR
  186. class RevisionNotFoundError(HfHubHTTPError):
  187. """
  188. Raised when trying to access a hf.co URL with a valid repository but an invalid
  189. revision.
  190. Attributes:
  191. repo_id (`str` or `None`):
  192. The repo id, if it could be determined from the request URL.
  193. repo_type (`str` or `None`):
  194. The repo type ("model", "dataset", or "space"), if it could be determined from the request URL.
  195. Example:
  196. ```py
  197. >>> from huggingface_hub import hf_hub_download
  198. >>> hf_hub_download('bert-base-cased', 'config.json', revision='<non-existent-revision>')
  199. (...)
  200. huggingface_hub.errors.RevisionNotFoundError: 404 Client Error. (Request ID: Mwhe_c3Kt650GcdKEFomX)
  201. Revision Not Found for url: https://huggingface.co/bert-base-cased/resolve/%3Cnon-existent-revision%3E/config.json.
  202. ```
  203. """
  204. repo_id: str | None = None
  205. repo_type: str | None = None
  206. # ENTRY ERRORS
  207. class EntryNotFoundError(Exception):
  208. """
  209. Raised when entry not found, either locally or remotely.
  210. Example:
  211. ```py
  212. >>> from huggingface_hub import hf_hub_download
  213. >>> hf_hub_download('bert-base-cased', '<non-existent-file>')
  214. (...)
  215. huggingface_hub.errors.RemoteEntryNotFoundError (...)
  216. >>> hf_hub_download('bert-base-cased', '<non-existent-file>', local_files_only=True)
  217. (...)
  218. huggingface_hub.utils.errors.LocalEntryNotFoundError (...)
  219. ```
  220. """
  221. class RemoteEntryNotFoundError(HfHubHTTPError, EntryNotFoundError):
  222. """
  223. Raised when trying to access a hf.co URL with a valid repository and revision
  224. but an invalid filename.
  225. Attributes:
  226. repo_id (`str` or `None`):
  227. The repo id, if it could be determined from the request URL.
  228. repo_type (`str` or `None`):
  229. The repo type ("model", "dataset", or "space"), if it could be determined from the request URL.
  230. Example:
  231. ```py
  232. >>> from huggingface_hub import hf_hub_download
  233. >>> hf_hub_download('bert-base-cased', '<non-existent-file>')
  234. (...)
  235. huggingface_hub.errors.EntryNotFoundError: 404 Client Error. (Request ID: 53pNl6M0MxsnG5Sw8JA6x)
  236. Entry Not Found for url: https://huggingface.co/bert-base-cased/resolve/main/%3Cnon-existent-file%3E.
  237. ```
  238. """
  239. repo_id: str | None = None
  240. repo_type: str | None = None
  241. class LocalEntryNotFoundError(FileNotFoundError, EntryNotFoundError):
  242. """
  243. Raised when trying to access a file or snapshot that is not on the disk when network is
  244. disabled or unavailable (connection issue). The entry may exist on the Hub.
  245. Example:
  246. ```py
  247. >>> from huggingface_hub import hf_hub_download
  248. >>> hf_hub_download('bert-base-cased', '<non-cached-file>', local_files_only=True)
  249. (...)
  250. huggingface_hub.errors.LocalEntryNotFoundError: Cannot find the requested files in the disk cache and outgoing traffic has been disabled. To enable hf.co look-ups and downloads online, set 'local_files_only' to False.
  251. ```
  252. """
  253. def __init__(self, message: str):
  254. super().__init__(message)
  255. # REQUEST ERROR
  256. class BadRequestError(HfHubHTTPError, ValueError):
  257. """
  258. Raised by `hf_raise_for_status` when the server returns a HTTP 400 error.
  259. Example:
  260. ```py
  261. >>> resp = httpx.post("hf.co/api/check", ...)
  262. >>> hf_raise_for_status(resp, endpoint_name="check")
  263. huggingface_hub.errors.BadRequestError: Bad request for check endpoint: {details} (Request ID: XXX)
  264. ```
  265. """
  266. # DDUF file format ERROR
  267. class DDUFError(Exception):
  268. """Base exception for errors related to the DDUF format."""
  269. class DDUFCorruptedFileError(DDUFError):
  270. """Exception thrown when the DDUF file is corrupted."""
  271. class DDUFExportError(DDUFError):
  272. """Base exception for errors during DDUF export."""
  273. class DDUFInvalidEntryNameError(DDUFExportError):
  274. """Exception thrown when the entry name is invalid."""
  275. # STRICT DATACLASSES ERRORS
  276. class StrictDataclassError(Exception):
  277. """Base exception for strict dataclasses."""
  278. class StrictDataclassDefinitionError(StrictDataclassError):
  279. """Exception thrown when a strict dataclass is defined incorrectly."""
  280. class StrictDataclassFieldValidationError(StrictDataclassError):
  281. """Exception thrown when a strict dataclass fails validation for a given field."""
  282. def __init__(self, field: str, cause: Exception):
  283. error_message = f"Validation error for field '{field}':"
  284. error_message += f"\n {cause.__class__.__name__}: {cause}"
  285. super().__init__(error_message)
  286. class StrictDataclassClassValidationError(StrictDataclassError):
  287. """Exception thrown when a strict dataclass fails validation on a class validator."""
  288. def __init__(self, validator: str, cause: Exception):
  289. error_message = f"Class validation error for validator '{validator}':"
  290. error_message += f"\n {cause.__class__.__name__}: {cause}"
  291. super().__init__(error_message)
  292. # XET ERRORS
  293. class XetError(Exception):
  294. """Base exception for errors related to Xet Storage."""
  295. class XetAuthorizationError(XetError):
  296. """Exception thrown when the user does not have the right authorization to use Xet Storage."""
  297. class XetRefreshTokenError(XetError):
  298. """Exception thrown when the refresh token is invalid."""
  299. class XetDownloadError(Exception):
  300. """Exception thrown when the download from Xet Storage fails."""
  301. # CLI ERRORS
  302. class CLIError(Exception):
  303. """CLI error with clean message (no traceback by default)."""
  304. class ConfirmationError(CLIError):
  305. """Raised when a confirmation prompt is declined (non-interactive mode)."""
  306. class CLIExtensionInstallError(CLIError):
  307. """Error during CLI extension installation."""