Skip to content

Commit

Permalink
Updated callback
Browse files Browse the repository at this point in the history
  • Loading branch information
kunwar-reworkd committed Dec 20, 2024
1 parent 5d59c93 commit 6b84f40
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
10 changes: 3 additions & 7 deletions sdk/harambe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
from harambe.contrib import WebHarness, playwright_harness


async def default_goto_error_cb(url: str, status: int):
async def default_callback(url: str, status: int):
raise GotoError(url, status)


Expand Down Expand Up @@ -453,7 +453,7 @@ async def run(
harness: WebHarness = playwright_harness,
evaluator: Optional[ExpressionEvaluator] = None,
observer: Optional[OutputObserver | List[OutputObserver]] = None,
goto_error_cb: Callable[[str, int], Awaitable[None]] = default_goto_error_cb,
callback: Callable[[str, int], Awaitable[None]] = default_callback,
**harness_options: Unpack[HarnessOptions],
) -> "SDK":
"""
Expand All @@ -479,10 +479,6 @@ async def run(
if isinstance(url, Path):
url = f"file://{url.resolve()}"

async def default_goto_error_cb(url: str, status: int):
raise GotoError(url, status)

error_callback = goto_error_cb or default_goto_error_cb
async with harness(**harness_options) as page_factory:
page = await page_factory()
sdk = SDK(
Expand All @@ -501,7 +497,7 @@ async def default_goto_error_cb(url: str, status: int):
if not harness_options.get("disable_go_to_url", False):
response = await page.goto(url)
if response.status >= 400:
await error_callback(url, response.status)
await callback(url, response.status)
elif isinstance(page, SoupPage):
page.url = url
await scraper(sdk, url, context)
Expand Down
4 changes: 2 additions & 2 deletions sdk/test/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ async def test_403_status_on_goto_with_custom_callback(
async def scrape(sdk: SDK, current_url, context) -> None:
await sdk.save_data({"key": "this shouldn't be saved if GotoError is raised"})

async def custom_error_handler(status_code, url, **kwargs):
async def custom_error_handler(url, status_code):
print(f"Handled {status_code} for {url} gracefully.")

error_callback = custom_error_handler
Expand All @@ -672,7 +672,7 @@ async def custom_error_handler(status_code, url, **kwargs):
schema={},
context={"status": "Open"},
observer=observer,
goto_error_cb=error_callback,
callback=error_callback,
)

# Ensure data is saved when error is handled (either with custom or no callback)
Expand Down

0 comments on commit 6b84f40

Please sign in to comment.