Repository navigation
Expand file tree
/
Copy pathunstructuredclienterror.py
More file actions
54 lines (45 loc) · 1.68 KB
/
Copy pathunstructuredclienterror.py
File metadata and controls
54 lines (45 loc) · 1.68 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
import httpx
from typing import Optional, Union
from unstructured_client.utils.declared_errors import (
DeclaredErrorFields,
parse_declared_error_fields,
)
class UnstructuredClientError(Exception):
"""The base class for all HTTP error responses."""
message: str
status_code: int
body: str
headers: httpx.Headers
raw_response: httpx.Response
retryable: Optional[bool]
error_type: Optional[str]
error_reason: Optional[str]
audience: Optional[str]
def __init__(
self, message: str, raw_response: httpx.Response, body: Optional[str] = None
):
self.message = message
self.status_code = raw_response.status_code
self.body = body if body is not None else raw_response.text
self.headers = raw_response.headers
self.raw_response = raw_response
# A success response that failed validation can carry a large body that
# declares nothing, so only error statuses are parsed.
declared = (
parse_declared_error_fields(self._raw_body())
if self.status_code >= 400
else DeclaredErrorFields()
)
self.retryable = declared.retryable
self.error_type = declared.error_type
self.error_reason = declared.error_reason
self.audience = declared.audience
def _raw_body(self) -> Union[str, bytes]:
# The response bytes keep the JSON encoding detection that decoded text loses.
try:
return self.raw_response.content
except httpx.ResponseNotRead:
return self.body
def __str__(self):
return self.message