-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathexceptions.py
More file actions
37 lines (23 loc) · 810 Bytes
/
exceptions.py
File metadata and controls
37 lines (23 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from json import JSONDecodeError
import httpx
class ReplicateException(Exception):
pass
class ModelError(ReplicateException):
"""An error from user's code in a model."""
class ReplicateError(ReplicateException):
"""An error from Replicate."""
class APIError(ReplicateError):
"""An error from the Replicate API."""
status: int
detail: str
def __init__(self, status: int, detail: str) -> None:
self.status = status
self.detail = detail
super().__init__(f"HTTP {status}: {detail}")
@classmethod
def from_response(cls, resp: httpx.Response) -> "APIError":
try:
return APIError(**resp.json())
except (JSONDecodeError, KeyError):
pass
return APIError(status=resp.status_code, detail=resp.text)