1+ from typing import Optional
2+
3+ import httpx
4+
5+
16class ReplicateException (Exception ):
27 """A base class for all Replicate exceptions."""
38
@@ -7,4 +12,84 @@ class ModelError(ReplicateException):
712
813
914class ReplicateError (ReplicateException ):
10- """An error from Replicate."""
15+ """
16+ An error from Replicate's API.
17+
18+ This class represents a problem details response as defined in RFC 7807.
19+ """
20+
21+ type : Optional [str ]
22+ """A URI that identifies the error type."""
23+
24+ title : Optional [str ]
25+ """A short, human-readable summary of the error."""
26+
27+ status : Optional [int ]
28+ """The HTTP status code."""
29+
30+ detail : Optional [str ]
31+ """A human-readable explanation specific to this occurrence of the error."""
32+
33+ instance : Optional [str ]
34+ """A URI that identifies the specific occurrence of the error."""
35+
36+ def __init__ (
37+ self ,
38+ type : Optional [str ] = None ,
39+ title : Optional [str ] = None ,
40+ status : Optional [int ] = None ,
41+ detail : Optional [str ] = None ,
42+ instance : Optional [str ] = None ,
43+ ) -> None :
44+ self .type = type
45+ self .title = title
46+ self .status = status
47+ self .detail = detail
48+ self .instance = instance
49+
50+ @classmethod
51+ def from_response (cls , response : httpx .Response ) -> "ReplicateError" :
52+ """Create a ReplicateError from an HTTP response."""
53+ try :
54+ data = response .json ()
55+ except ValueError :
56+ data = {}
57+
58+ return cls (
59+ type = data .get ("type" ),
60+ title = data .get ("title" ),
61+ detail = data .get ("detail" ),
62+ status = response .status_code ,
63+ instance = data .get ("instance" ),
64+ )
65+
66+ def to_dict (self ) -> dict :
67+ return {
68+ key : value
69+ for key , value in {
70+ "type" : self .type ,
71+ "title" : self .title ,
72+ "status" : self .status ,
73+ "detail" : self .detail ,
74+ "instance" : self .instance ,
75+ }.items ()
76+ if value is not None
77+ }
78+
79+ def __str__ (self ) -> str :
80+ return "ReplicateError Details:\n " + "\n " .join (
81+ [f"{ key } : { value } " for key , value in self .to_dict ().items ()]
82+ )
83+
84+ def __repr__ (self ) -> str :
85+ class_name = self .__class__ .__name__
86+ params = ", " .join (
87+ [
88+ f"type={ repr (self .type )} " ,
89+ f"title={ repr (self .title )} " ,
90+ f"status={ repr (self .status )} " ,
91+ f"detail={ repr (self .detail )} " ,
92+ f"instance={ repr (self .instance )} " ,
93+ ]
94+ )
95+ return f"{ class_name } ({ params } )"
0 commit comments