Skip to content

Commit

Permalink
method record exception (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
cle-b authored Dec 1, 2024
1 parent 9cc392e commit b45422f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 70 deletions.
2 changes: 1 addition & 1 deletion httpdbg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from httpdbg.records import HTTPRecords


__version__ = "0.28.2"
__version__ = "0.28.3"

__all__ = ["httprecord", "HTTPRecords"]
14 changes: 5 additions & 9 deletions httpdbg/hooks/aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from collections.abc import Callable
from contextlib import contextmanager
import traceback
from typing import Generator
Expand All @@ -7,11 +8,10 @@
from httpdbg.hooks.utils import decorate
from httpdbg.hooks.utils import undecorate
from httpdbg.initiator import httpdbg_initiator
from httpdbg.records import HTTPRecord
from httpdbg.records import HTTPRecords


def set_hook_for_aiohttp_async(records, method):
def set_hook_for_aiohttp_async(records: HTTPRecords, method: Callable):
async def hook(*args, **kwargs):
initiator = None
try:
Expand All @@ -25,13 +25,9 @@ async def hook(*args, **kwargs):

if "str_or_url" in callargs:
if initiator:
record = HTTPRecord()

record.initiator = initiator
record.url = str(callargs["str_or_url"])
record.exception = ex

records.requests[record.id] = record
records.add_new_record_exception(
initiator, str(callargs["str_or_url"]), ex
)
raise

return hook
Expand Down
26 changes: 9 additions & 17 deletions httpdbg/hooks/httpx.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from collections.abc import Callable
from contextlib import contextmanager
import traceback
from typing import Generator
Expand All @@ -7,11 +8,10 @@
from httpdbg.hooks.utils import decorate
from httpdbg.hooks.utils import undecorate
from httpdbg.initiator import httpdbg_initiator
from httpdbg.records import HTTPRecord
from httpdbg.records import HTTPRecords


def set_hook_for_httpx_async(records, method):
def set_hook_for_httpx_async(records: HTTPRecords, method: Callable):
async def hook(*args, **kwargs):
initiator = None
try:
Expand All @@ -25,19 +25,15 @@ async def hook(*args, **kwargs):

if "url" in callargs:
if initiator:
record = HTTPRecord()

record.initiator = initiator
record.url = str(callargs["url"])
record.exception = ex

records.requests[record.id] = record
records.add_new_record_exception(
initiator, str(callargs["url"]), ex
)
raise

return hook


def set_hook_for_httpx(records, method):
def set_hook_for_httpx(records: HTTPRecords, method: Callable):
def hook(*args, **kwargs):
initiator = None
try:
Expand All @@ -51,13 +47,9 @@ def hook(*args, **kwargs):

if "url" in callargs:
if initiator:
record = HTTPRecord()

record.initiator = initiator
record.url = str(callargs["url"])
record.exception = ex

records.requests[record.id] = record
records.add_new_record_exception(
initiator, str(callargs["url"]), ex
)
raise

return hook
Expand Down
14 changes: 5 additions & 9 deletions httpdbg/hooks/requests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from collections.abc import Callable
from contextlib import contextmanager
import traceback
from typing import Generator
Expand All @@ -7,11 +8,10 @@
from httpdbg.hooks.utils import decorate
from httpdbg.hooks.utils import undecorate
from httpdbg.initiator import httpdbg_initiator
from httpdbg.records import HTTPRecord
from httpdbg.records import HTTPRecords


def set_hook_for_requests(records, method):
def set_hook_for_requests(records: HTTPRecords, method: Callable):
def hook(*args, **kwargs):
initiator = None
try:
Expand All @@ -25,13 +25,9 @@ def hook(*args, **kwargs):

if "url" in callargs:
if initiator:
record = HTTPRecord()

record.initiator = initiator
record.url = str(callargs["url"])
record.exception = ex

records.requests[record.id] = record
records.add_new_record_exception(
initiator, str(callargs["url"]), ex
)
raise

return hook
Expand Down
33 changes: 8 additions & 25 deletions httpdbg/hooks/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ def hook(self, address):
if not isinstance(
ex, (BlockingIOError, OSError)
): # BlockingIOError for async, OSError for ipv6
record = HTTPRecord()
record.initiator = records.get_initiator()
record.exception = ex
records.requests[record.id] = record
initiator = records.get_initiator()
records.add_new_record_exception(initiator, "", ex)
raise

return r
Expand All @@ -65,13 +63,8 @@ def hook(sock, *args, **kwargs):
try:
sslsocket = method(sock, *args, **kwargs)
except Exception as ex:
record = HTTPRecord()

record.initiator = records.get_initiator()
record.exception = ex

records.requests[record.id] = record

initiator = records.get_initiator()
records.add_new_record_exception(initiator, "", ex)
raise

logger().info(
Expand All @@ -95,13 +88,8 @@ def hook(self, sock, *args, **kwargs):
try:
sslsocket = method(self, sock, *args, **kwargs)
except Exception as ex:
record = HTTPRecord()

record.initiator = records.get_initiator()
record.exception = ex

records.requests[record.id] = record

initiator = records.get_initiator()
records.add_new_record_exception(initiator, "", ex)
raise

logger().info(
Expand All @@ -125,13 +113,8 @@ def hook(self, *args, **kwargs):
try:
sslobject = method(self, *args, **kwargs)
except Exception as ex:
record = HTTPRecord()
record.initiator = records.get_initiator()

record.exception = ex

records.requests[record.id] = record

initiator = records.get_initiator()
records.add_new_record_exception(initiator, "", ex)
raise

logger().info(
Expand Down
14 changes: 5 additions & 9 deletions httpdbg/hooks/urllib3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from collections.abc import Callable
from contextlib import contextmanager
import traceback
from typing import Generator
Expand All @@ -7,11 +8,10 @@
from httpdbg.hooks.utils import decorate
from httpdbg.hooks.utils import undecorate
from httpdbg.initiator import httpdbg_initiator
from httpdbg.records import HTTPRecord
from httpdbg.records import HTTPRecords


def set_hook_for_urllib3(records, method):
def set_hook_for_urllib3(records: HTTPRecords, method: Callable):
def hook(*args, **kwargs):
initiator = None
try:
Expand All @@ -25,13 +25,9 @@ def hook(*args, **kwargs):

if "url" in callargs:
if initiator:
record = HTTPRecord()

record.initiator = initiator
record.url = str(callargs["url"])
record.exception = ex

records.requests[record.id] = record
records.add_new_record_exception(
initiator, str(callargs["url"]), ex
)
raise

return hook
Expand Down
10 changes: 10 additions & 0 deletions httpdbg/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,3 +413,13 @@ def del_socket_data(self, obj):
logger().info(f"SocketRawData del id={id(obj)}")
self._sockets[id(obj)] = None
del self._sockets[id(obj)]

def add_new_record_exception(
self, initiator: Initiator, url: str, exception: Exception
) -> HTTPRecord:
new_record = HTTPRecord()
new_record.url = url
new_record.initiator = initiator
new_record.exception = exception
self.requests[new_record.id] = new_record
return new_record

0 comments on commit b45422f

Please sign in to comment.