66from typing import TYPE_CHECKING
77
88if TYPE_CHECKING :
9- from componentize_py_types import Err as WitErr
10-
119 from .response import FastlyResponse
1210
1311# Runtime imports needed for error mappings at module level
1412from wit_world .imports import http_req
1513from wit_world .imports import types as wit_types
1614from wit_world .imports .http_req import SendErrorDetail
1715
16+ from fastly_compute .exceptions import FastlyError
17+ from fastly_compute .exceptions .http_req import ErrorWithDetail
18+ from fastly_compute .exceptions .types .error import (
19+ CannotRead ,
20+ HttpHeadTooLarge ,
21+ HttpIncomplete ,
22+ HttpInvalid ,
23+ HttpInvalidStatus ,
24+ HttpUser ,
25+ )
26+
1827
1928def _map_error_to_exception (
2029 error : object ,
@@ -59,19 +68,19 @@ def __init__(
5968 self .request : http_req .Request | None = request
6069
6170 @classmethod
62- def from_http_req_error (
63- cls , err : WitErr [ http_req . ErrorWithDetail ] , operation : str
71+ def from_detailed_error (
72+ cls , err : ErrorWithDetail , operation : str
6473 ) -> RequestException :
65- """Create appropriate exception from http_req WIT error .
74+ """Create a ``requests`` exception from an ErrorWithDetail .
6675
6776 Args:
68- err: WIT Err exception containing ErrorWithDetail
77+ err: The error to map from
6978 operation: Description of what operation failed
7079
7180 Returns:
7281 Appropriate RequestException subclass instance
7382 """
74- error_with_detail = err .value
83+ error_with_detail = err .args [ 0 ]
7584
7685 # Try detailed error classification first; this is not guaranteed
7786 # to be present in all cases.
@@ -92,21 +101,19 @@ def from_http_req_error(
92101 )
93102
94103 @classmethod
95- def from_wit_error (
96- cls , err : WitErr [wit_types .Error ], operation : str
97- ) -> RequestException :
98- """Create appropriate exception from generic WIT error.
104+ def from_fastly_error (cls , err : FastlyError , operation : str ) -> RequestException :
105+ """Create a ``requests`` exception from a FastlyError or subclass.
99106
100107 Args:
101- err: WIT Err exception containing generic Error
108+ err: The error to map from
102109 operation: Description of what operation failed
103110
104111 Returns:
105112 Appropriate RequestException subclass instance
106113 """
107114 return _map_error_to_exception (
108- err . value ,
109- WIT_ERROR_MAPPINGS ,
115+ err ,
116+ FASTLY_ERROR_MAPPINGS ,
110117 f"Operation { operation } failed" ,
111118 cls ,
112119 )
@@ -200,3 +207,17 @@ class StreamConsumedError(RequestException, TypeError):
200207 }
201208 )
202209)
210+
211+ # Map FastlyErrors to the errors `requests` returns.
212+ FASTLY_ERROR_MAPPINGS : MappingProxyType [type [FastlyError ], type [RequestException ]] = (
213+ MappingProxyType (
214+ {
215+ HttpInvalid : HTTPError ,
216+ HttpUser : HTTPError ,
217+ HttpIncomplete : HTTPError ,
218+ HttpHeadTooLarge : HTTPError ,
219+ HttpInvalidStatus : HTTPError ,
220+ CannotRead : ConnectionError ,
221+ }
222+ )
223+ )
0 commit comments