|
42 | 42 | ], |
43 | 43 | ] |
44 | 44 |
|
| 45 | +_HTTP_AUTH_ERRORS: dict[int, str] = {401: "unauthorized", 403: "forbidden"} |
| 46 | + |
45 | 47 |
|
46 | 48 | class OFREPProvider(AbstractProvider): |
47 | 49 | def __init__( |
@@ -183,57 +185,46 @@ def _handle_error(self, exception: requests.RequestException) -> NoReturn: |
183 | 185 | except JSONDecodeError: |
184 | 186 | raise ParseError(str(exception)) from exception |
185 | 187 |
|
186 | | - self._raise_for_error_code(data, exception) |
| 188 | + error_code = ErrorCode(data["errorCode"]) |
| 189 | + error_details = data["errorDetails"] |
| 190 | + |
| 191 | + if error_code == ErrorCode.PARSE_ERROR: |
| 192 | + raise ParseError(error_details) from exception |
| 193 | + if error_code == ErrorCode.TARGETING_KEY_MISSING: |
| 194 | + raise TargetingKeyMissingError(error_details) from exception |
| 195 | + if error_code == ErrorCode.INVALID_CONTEXT: |
| 196 | + raise InvalidContextError(error_details) from exception |
| 197 | + if error_code == ErrorCode.GENERAL: |
| 198 | + raise GeneralError(error_details) from exception |
| 199 | + |
| 200 | + raise OpenFeatureError(error_code, error_details) from exception |
187 | 201 |
|
188 | 202 | def _raise_for_http_status( |
189 | 203 | self, |
190 | 204 | response: requests.Response, |
191 | 205 | exception: requests.RequestException, |
192 | 206 | ) -> None: |
193 | | - if response.status_code == 429: |
| 207 | + status = response.status_code |
| 208 | + |
| 209 | + if status == 429: |
194 | 210 | retry_after = response.headers.get("Retry-After") |
195 | 211 | self.retry_after = _parse_retry_after(retry_after) |
196 | 212 | raise GeneralError( |
197 | 213 | f"Rate limited, retry after: {retry_after}" |
198 | 214 | ) from exception |
199 | | - |
200 | | - if response.status_code == 401: |
201 | | - raise OpenFeatureError(ErrorCode.GENERAL, "unauthorized") from exception |
202 | | - |
203 | | - if response.status_code == 403: |
204 | | - raise OpenFeatureError(ErrorCode.GENERAL, "forbidden") from exception |
205 | | - |
206 | | - if response.status_code == 404: |
| 215 | + elif status in _HTTP_AUTH_ERRORS: |
| 216 | + raise OpenFeatureError( |
| 217 | + ErrorCode.GENERAL, _HTTP_AUTH_ERRORS[status] |
| 218 | + ) from exception |
| 219 | + elif status == 404: |
207 | 220 | try: |
208 | | - data = response.json() |
209 | | - error_details = data["errorDetails"] |
210 | | - except JSONDecodeError: |
| 221 | + error_details = response.json()["errorDetails"] |
| 222 | + except (JSONDecodeError, KeyError): |
211 | 223 | error_details = response.text |
212 | 224 | raise FlagNotFoundError(error_details) from exception |
213 | | - |
214 | | - if response.status_code > 400: |
| 225 | + elif status > 400: |
215 | 226 | raise OpenFeatureError(ErrorCode.GENERAL, response.text) from exception |
216 | 227 |
|
217 | | - def _raise_for_error_code( |
218 | | - self, |
219 | | - data: dict[str, Any], |
220 | | - exception: requests.RequestException, |
221 | | - ) -> NoReturn: |
222 | | - error_code = ErrorCode(data["errorCode"]) |
223 | | - error_details = data["errorDetails"] |
224 | | - |
225 | | - if error_code == ErrorCode.PARSE_ERROR: |
226 | | - raise ParseError(error_details) from exception |
227 | | - if error_code == ErrorCode.TARGETING_KEY_MISSING: |
228 | | - raise TargetingKeyMissingError(error_details) from exception |
229 | | - if error_code == ErrorCode.INVALID_CONTEXT: |
230 | | - raise InvalidContextError(error_details) from exception |
231 | | - if error_code == ErrorCode.GENERAL: |
232 | | - raise GeneralError(error_details) from exception |
233 | | - |
234 | | - raise OpenFeatureError(error_code, error_details) from exception |
235 | | - |
236 | | - |
237 | 228 | def _build_request_data( |
238 | 229 | evaluation_context: Optional[EvaluationContext], |
239 | 230 | ) -> dict[str, Any]: |
|
0 commit comments