Skip to content

Commit e5438a9

Browse files
chore(internal): construct error properties instead of using the raw response (#204)
1 parent 9757d20 commit e5438a9

File tree

1 file changed

+122
-49
lines changed

1 file changed

+122
-49
lines changed

src/orb/_exceptions.py

+122-49
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import httpx
99

1010
from ._utils import is_mapping
11+
from ._models import construct_type
1112

1213
__all__ = [
1314
"BadRequestError",
@@ -136,10 +137,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
136137
data = cast(Mapping[str, object], body if is_mapping(body) else {})
137138
super().__init__(message, response=response, body=body)
138139

139-
self.status = cast(Any, data.get("status"))
140-
self.type = cast(Any, data.get("type"))
141-
self.detail = cast(Any, data.get("detail"))
142-
self.title = cast(Any, data.get("title"))
140+
self.status = cast(Any, construct_type(type_=Literal[400], value=data.get("status")))
141+
self.type = cast(
142+
Any,
143+
construct_type(
144+
type_=Literal["https://docs.withorb.com/reference/error-responses#400-constraint-violation"],
145+
value=data.get("type"),
146+
),
147+
)
148+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
149+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
143150

144151

145152
class DuplicateResourceCreation(BadRequestError):
@@ -155,10 +162,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
155162
data = cast(Mapping[str, object], body if is_mapping(body) else {})
156163
super().__init__(message, response=response, body=body)
157164

158-
self.status = cast(Any, data.get("status"))
159-
self.type = cast(Any, data.get("type"))
160-
self.detail = cast(Any, data.get("detail"))
161-
self.title = cast(Any, data.get("title"))
165+
self.status = cast(Any, construct_type(type_=Literal[400], value=data.get("status")))
166+
self.type = cast(
167+
Any,
168+
construct_type(
169+
type_=Literal["https://docs.withorb.com/reference/error-responses#400-duplicate-resource-creation"],
170+
value=data.get("type"),
171+
),
172+
)
173+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
174+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
162175

163176

164177
class FeatureNotAvailable(BadRequestError):
@@ -174,10 +187,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
174187
data = cast(Mapping[str, object], body if is_mapping(body) else {})
175188
super().__init__(message, response=response, body=body)
176189

177-
self.status = cast(Any, data.get("status"))
178-
self.type = cast(Any, data.get("type"))
179-
self.detail = cast(Any, data.get("detail"))
180-
self.title = cast(Any, data.get("title"))
190+
self.status = cast(Any, construct_type(type_=Literal[400], value=data.get("status")))
191+
self.type = cast(
192+
Any,
193+
construct_type(
194+
type_=Literal["https://docs.withorb.com/reference/error-responses#404-feature-not-available"],
195+
value=data.get("type"),
196+
),
197+
)
198+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
199+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
181200

182201

183202
class RequestValidationError(BadRequestError):
@@ -195,11 +214,17 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
195214
data = cast(Mapping[str, object], body if is_mapping(body) else {})
196215
super().__init__(message, response=response, body=body)
197216

198-
self.status = cast(Any, data.get("status"))
199-
self.type = cast(Any, data.get("type"))
200-
self.validation_errors = cast(Any, data.get("validation_errors"))
201-
self.detail = cast(Any, data.get("detail"))
202-
self.title = cast(Any, data.get("title"))
217+
self.status = cast(Any, construct_type(type_=Literal[400], value=data.get("status")))
218+
self.type = cast(
219+
Any,
220+
construct_type(
221+
type_=Literal["https://docs.withorb.com/reference/error-responses#400-request-validation-errors"],
222+
value=data.get("type"),
223+
),
224+
)
225+
self.validation_errors = cast(Any, construct_type(type_=List[object], value=data.get("validation_errors")))
226+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
227+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
203228

204229

205230
class OrbAuthenticationError(AuthenticationError):
@@ -215,10 +240,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
215240
data = cast(Mapping[str, object], body if is_mapping(body) else {})
216241
super().__init__(message, response=response, body=body)
217242

218-
self.status = cast(Any, data.get("status"))
219-
self.type = cast(Any, data.get("type"))
220-
self.detail = cast(Any, data.get("detail"))
221-
self.title = cast(Any, data.get("title"))
243+
self.status = cast(Any, construct_type(type_=Literal[401], value=data.get("status")))
244+
self.type = cast(
245+
Any,
246+
construct_type(
247+
type_=Literal["https://docs.withorb.com/reference/error-responses#401-authentication-error"],
248+
value=data.get("type"),
249+
),
250+
)
251+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
252+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
222253

223254

224255
class ResourceNotFound(NotFoundError):
@@ -234,10 +265,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
234265
data = cast(Mapping[str, object], body if is_mapping(body) else {})
235266
super().__init__(message, response=response, body=body)
236267

237-
self.status = cast(Any, data.get("status"))
238-
self.title = cast(Any, data.get("title"))
239-
self.type = cast(Any, data.get("type"))
240-
self.detail = cast(Any, data.get("detail"))
268+
self.status = cast(Any, construct_type(type_=Literal[404], value=data.get("status")))
269+
self.title = cast(Any, construct_type(type_=str, value=data.get("title")))
270+
self.type = cast(
271+
Any,
272+
construct_type(
273+
type_=Literal["https://docs.withorb.com/reference/error-responses#404-resource-not-found"],
274+
value=data.get("type"),
275+
),
276+
)
277+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
241278

242279

243280
class URLNotFound(NotFoundError):
@@ -253,10 +290,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
253290
data = cast(Mapping[str, object], body if is_mapping(body) else {})
254291
super().__init__(message, response=response, body=body)
255292

256-
self.status = cast(Any, data.get("status"))
257-
self.type = cast(Any, data.get("type"))
258-
self.detail = cast(Any, data.get("detail"))
259-
self.title = cast(Any, data.get("title"))
293+
self.status = cast(Any, construct_type(type_=Literal[404], value=data.get("status")))
294+
self.type = cast(
295+
Any,
296+
construct_type(
297+
type_=Literal["https://docs.withorb.com/reference/error-responses#404-url-not-found"],
298+
value=data.get("type"),
299+
),
300+
)
301+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
302+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
260303

261304

262305
class ResourceConflict(ConflictError):
@@ -272,10 +315,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
272315
data = cast(Mapping[str, object], body if is_mapping(body) else {})
273316
super().__init__(message, response=response, body=body)
274317

275-
self.status = cast(Any, data.get("status"))
276-
self.type = cast(Any, data.get("type"))
277-
self.detail = cast(Any, data.get("detail"))
278-
self.title = cast(Any, data.get("title"))
318+
self.status = cast(Any, construct_type(type_=Literal[409], value=data.get("status")))
319+
self.type = cast(
320+
Any,
321+
construct_type(
322+
type_=Literal["https://docs.withorb.com/reference/error-responses#409-resource-conflict"],
323+
value=data.get("type"),
324+
),
325+
)
326+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
327+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
279328

280329

281330
class RequestTooLarge(APIStatusError):
@@ -291,10 +340,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
291340
data = cast(Mapping[str, object], body if is_mapping(body) else {})
292341
super().__init__(message, response=response, body=body)
293342

294-
self.status = cast(Any, data.get("status"))
295-
self.type = cast(Any, data.get("type"))
296-
self.detail = cast(Any, data.get("detail"))
297-
self.title = cast(Any, data.get("title"))
343+
self.status = cast(Any, construct_type(type_=Literal[413], value=data.get("status")))
344+
self.type = cast(
345+
Any,
346+
construct_type(
347+
type_=Literal["https://docs.withorb.com/reference/error-responses#413-request-too-large"],
348+
value=data.get("type"),
349+
),
350+
)
351+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
352+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
298353

299354

300355
class ResourceTooLarge(APIStatusError):
@@ -310,10 +365,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
310365
data = cast(Mapping[str, object], body if is_mapping(body) else {})
311366
super().__init__(message, response=response, body=body)
312367

313-
self.status = cast(Any, data.get("status"))
314-
self.type = cast(Any, data.get("type"))
315-
self.detail = cast(Any, data.get("detail"))
316-
self.title = cast(Any, data.get("title"))
368+
self.status = cast(Any, construct_type(type_=Literal[413], value=data.get("status")))
369+
self.type = cast(
370+
Any,
371+
construct_type(
372+
type_=Literal["https://docs.withorb.com/reference/error-responses#413-resource-too-large"],
373+
value=data.get("type"),
374+
),
375+
)
376+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
377+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
317378

318379

319380
class TooManyRequests(RateLimitError):
@@ -329,10 +390,16 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
329390
data = cast(Mapping[str, object], body if is_mapping(body) else {})
330391
super().__init__(message, response=response, body=body)
331392

332-
self.status = cast(Any, data.get("status"))
333-
self.type = cast(Any, data.get("type"))
334-
self.detail = cast(Any, data.get("detail"))
335-
self.title = cast(Any, data.get("title"))
393+
self.status = cast(Any, construct_type(type_=Literal[429], value=data.get("status")))
394+
self.type = cast(
395+
Any,
396+
construct_type(
397+
type_=Literal["https://docs.withorb.com/reference/error-responses#429-too-many-requests"],
398+
value=data.get("type"),
399+
),
400+
)
401+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
402+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))
336403

337404

338405
class OrbInternalServerError(InternalServerError):
@@ -348,7 +415,13 @@ def __init__(self, message: str, *, body: object, response: httpx.Response) -> N
348415
data = cast(Mapping[str, object], body if is_mapping(body) else {})
349416
super().__init__(message, response=response, body=body)
350417

351-
self.status = cast(Any, data.get("status"))
352-
self.type = cast(Any, data.get("type"))
353-
self.detail = cast(Any, data.get("detail"))
354-
self.title = cast(Any, data.get("title"))
418+
self.status = cast(Any, construct_type(type_=Literal[500], value=data.get("status")))
419+
self.type = cast(
420+
Any,
421+
construct_type(
422+
type_=Literal["https://docs.withorb.com/reference/error-responses#500-internal-server-error"],
423+
value=data.get("type"),
424+
),
425+
)
426+
self.detail = cast(Any, construct_type(type_=Optional[str], value=data.get("detail")))
427+
self.title = cast(Any, construct_type(type_=Optional[str], value=data.get("title")))

0 commit comments

Comments
 (0)