|
10 | 10 | from enum import Enum |
11 | 11 | from typing import TYPE_CHECKING, Any, AsyncIterator, Dict, Iterator, List, Optional |
12 | 12 |
|
13 | | -from .errors import JobError |
| 13 | +from .errors import APIError, JobError |
14 | 14 | from .types import CrawlDownloadFormat, CrawlJob |
15 | 15 |
|
16 | 16 | if TYPE_CHECKING: |
@@ -64,7 +64,7 @@ def crawl( |
64 | 64 | api_url, crawl_delay, url_crawl_pattern, url_process_pattern, |
65 | 65 | obey_robots, use_proxies, custom_headers, |
66 | 66 | ) |
67 | | - client.request("GET", client.crawler_url, params=params) |
| 67 | + _raise_on_create_error(client.request("GET", client.crawler_url, params=params)) |
68 | 68 |
|
69 | 69 | yield CrawlEvent( |
70 | 70 | event_type=CrawlEventType.JOB_CREATED, |
@@ -103,6 +103,17 @@ def crawl( |
103 | 103 | time.sleep(poll_interval) |
104 | 104 |
|
105 | 105 |
|
| 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 | + |
106 | 117 | def _build_crawl_params(client: Any, job_name: str, site: str, max_to_crawl: int, |
107 | 118 | max_to_process: int, hops: int, restrict_domain: bool, |
108 | 119 | api_url: str, crawl_delay: float, url_crawl_pattern: Optional[str], |
@@ -177,7 +188,7 @@ async def crawl_async( |
177 | 188 | api_url, crawl_delay, url_crawl_pattern, url_process_pattern, |
178 | 189 | obey_robots, use_proxies, custom_headers, |
179 | 190 | ) |
180 | | - await client.request("GET", client.crawler_url, params=params) |
| 191 | + _raise_on_create_error(await client.request("GET", client.crawler_url, params=params)) |
181 | 192 |
|
182 | 193 | yield CrawlEvent( |
183 | 194 | event_type=CrawlEventType.JOB_CREATED, |
|
0 commit comments