Skip to content

Commit 653915b

Browse files
shixishclaude
andcommitted
fix(crawl): raise APIError when create returns HTTP 200 with errorCode
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 432ce4a commit 653915b

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎src/diffbot/crawl.py‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from enum import Enum
1111
from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, Iterator, List, Optional
1212

13-
from .errors import JobError
13+
from .errors import APIError, JobError
1414
from .types import CrawlDownloadFormat, CrawlJob
1515

1616
if TYPE_CHECKING:
@@ -64,7 +64,7 @@ def crawl(
6464
api_url, crawl_delay, url_crawl_pattern, url_process_pattern,
6565
obey_robots, use_proxies, custom_headers,
6666
)
67-
client.request("GET", client.crawler_url, params=params)
67+
_raise_on_create_error(client.request("GET", client.crawler_url, params=params))
6868

6969
yield CrawlEvent(
7070
event_type=CrawlEventType.JOB_CREATED,
@@ -103,6 +103,17 @@ def crawl(
103103
time.sleep(poll_interval)
104104

105105

106+
def _raise_on_create_error(response: Any) -> None:
107+
"""The crawler reports some create failures (e.g. a job name over 30 chars) as HTTP 200
108+
with ``errorCode``/``error`` in the body; surface them instead of returning JOB_CREATED."""
109+
try:
110+
data = response.json()
111+
except ValueError:
112+
return
113+
if isinstance(data, dict) and data.get("errorCode"):
114+
raise APIError(200, json.dumps({"message": data.get("error") or f"errorCode {data['errorCode']}"}))
115+
116+
106117
def _build_crawl_params(client: Any, job_name: str, site: str, max_to_crawl: int,
107118
max_to_process: int, hops: int, restrict_domain: bool,
108119
api_url: str, crawl_delay: float, url_crawl_pattern: Optional[str],
@@ -177,7 +188,7 @@ async def crawl_async(
177188
api_url, crawl_delay, url_crawl_pattern, url_process_pattern,
178189
obey_robots, use_proxies, custom_headers,
179190
)
180-
await client.request("GET", client.crawler_url, params=params)
191+
_raise_on_create_error(await client.request("GET", client.crawler_url, params=params))
181192

182193
yield CrawlEvent(
183194
event_type=CrawlEventType.JOB_CREATED,

‎tests/test_crawl.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,23 @@ async def test_async_crawl_watch_uses_download_for_urls():
236236
def test_crawl_get_job_missing_is_empty_dict():
237237
db = make_client(lambda r: httpx.Response(200, json={"jobs": []}))
238238
assert db.crawl_get_job("nope") == {}
239+
240+
241+
def test_crawl_raises_when_create_returns_200_with_error_code():
242+
def handler(request: httpx.Request) -> httpx.Response:
243+
return httpx.Response(200, json={"errorCode": 505, "error": "name is over 30 chars"})
244+
245+
db = make_client(handler)
246+
with pytest.raises(APIError, match="name is over 30 chars"):
247+
next(db.crawl("https://example.com", job_name="x" * 31))
248+
249+
250+
@pytest.mark.anyio
251+
async def test_async_crawl_raises_when_create_returns_200_with_error_code():
252+
def handler(request: httpx.Request) -> httpx.Response:
253+
return httpx.Response(200, json={"errorCode": 505, "error": "name is over 30 chars"})
254+
255+
db = DiffbotAsync(token="test-token", transport=httpx.MockTransport(handler))
256+
with pytest.raises(APIError, match="name is over 30 chars"):
257+
async for _ in db.crawl("https://example.com", job_name="x" * 31):
258+
pass

0 commit comments

Comments
 (0)