Hansimov commited on
Commit
a1d960f
1 Parent(s): 862427b

:gem: [Feature] New HfApiException: INVALID_API_KEY_ERROR

Browse files
Files changed (1) hide show
  1. networks/exceptions.py +31 -0
networks/exceptions.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import http
2
+
3
+ from typing import Optional
4
+
5
+ from fastapi import HTTPException, status
6
+
7
+
8
+ class HfApiException(Exception):
9
+ def __init__(
10
+ self,
11
+ status_code: int,
12
+ detail: Optional[str] = None,
13
+ ) -> None:
14
+ if detail is None:
15
+ self.detail = http.HTTPStatus(status_code).phrase
16
+ else:
17
+ self.detail = detail
18
+ self.status_code = status_code
19
+
20
+ def __repr__(self) -> str:
21
+ class_name = self.__class__.__name__
22
+ return f"{class_name}(status_code={self.status_code!r}, detail={self.detail!r})"
23
+
24
+ def __str__(self) -> str:
25
+ return self.__repr__()
26
+
27
+
28
+ INVALID_API_KEY_ERROR = HfApiException(
29
+ status_code=status.HTTP_403_FORBIDDEN,
30
+ detail="Invalid API Key",
31
+ )