Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
burnash committed Nov 11, 2024
1 parent 9ea970c commit 85b8f77
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 15 additions & 7 deletions dlt/sources/helpers/rest_client/error_handlers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import Optional, Dict, Any
from typing import Optional, Type, Any
from types import TracebackType

#
from requests.exceptions import HTTPError


def find_http_error(exception: Exception) -> Optional[Dict[str, Any]]:
def find_http_error(exception: BaseException) -> Optional[HTTPError]:
"""Recursively searches through exception chain to find and extract HTTPError.
Args:
Expand All @@ -22,7 +25,7 @@ def find_http_error(exception: Exception) -> Optional[Dict[str, Any]]:
raise
"""

def _get_next_cause(e: Exception) -> Optional[Exception]:
def _get_next_cause(e: BaseException) -> Optional[BaseException]:
# Handle both __cause__ (from raise ... from) and __context__ (from bare raise)
return e.__cause__ if e.__cause__ is not None else e.__context__

Expand All @@ -36,23 +39,28 @@ def _get_next_cause(e: Exception) -> Optional[Exception]:


class HTTPErrorCatcher:
def __init__(self):
def __init__(self) -> None:
self.http_error: Optional[HTTPError] = None

def __enter__(self) -> "HTTPErrorCatcher":
return self

def __exit__(self, exc_type, exc_val, exc_tb) -> bool:
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> bool:
if exc_val is not None:
self.http_error = find_http_error(exc_val)
# Suppress exception if we found an HTTPError
return bool(self.http_error)
return False

def __bool__(self):
def __bool__(self) -> bool:
return self.http_error is not None

def __getattr__(self, attr):
def __getattr__(self, attr: str) -> Any:
if self.http_error:
return getattr(self.http_error, attr)
raise AttributeError(f"{attr} not found in {self.__class__.__name__}")
Expand Down
4 changes: 2 additions & 2 deletions tests/sources/rest_api/integration/test_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from unittest import mock

import pytest
from requests import Request, Response, Session as BaseSession
from requests import Request, Response

from dlt.sources.helpers.requests import Session

Expand Down Expand Up @@ -406,4 +406,4 @@ def test_catch_http_error_without_error(mock_api_server):
assert not http_error
assert len(data) > 0
assert data[0]["id"] == 0
assert data[0]["title"] == "Post 0"
assert data[0]["title"] == "Post 0"

0 comments on commit 85b8f77

Please sign in to comment.