Skip to content

Commit 756ae86

Browse files
committed
If Retry has a message, use it as retry reason
1 parent 728953e commit 756ae86

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

scrapy_poet/spidermiddlewares.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def process_spider_exception(
2424
new_request_or_none = get_retry_request(
2525
response.request,
2626
spider=spider,
27-
reason="page_object_retry",
27+
reason=str(exception) or "page_object_retry",
2828
)
2929
if not new_request_or_none:
3030
return []

tests/test_retries.py

+34
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@ def parse(self, response, page: SamplePage):
5353
_assert_all_unique_instances(page_response_instances)
5454

5555

56+
@inlineCallbacks
57+
def test_retry_reason():
58+
retries = deque([True, False])
59+
items, page_instances, page_response_instances = [], [], []
60+
61+
with MockServer(EchoResource) as server:
62+
63+
class SamplePage(WebPage):
64+
def to_item(self):
65+
page_instances.append(self)
66+
page_response_instances.append(self.response)
67+
if retries.popleft():
68+
raise Retry("foo")
69+
return {"foo": "bar"}
70+
71+
class TestSpider(BaseSpider):
72+
def start_requests(self):
73+
yield Request(server.root_url, callback=self.parse)
74+
75+
def parse(self, response, page: SamplePage):
76+
items.append(page.to_item())
77+
78+
crawler = make_crawler(TestSpider)
79+
yield crawler.crawl()
80+
81+
assert items == [{"foo": "bar"}]
82+
assert crawler.stats.get_value("downloader/request_count") == 2
83+
assert crawler.stats.get_value("retry/count") == 1
84+
assert crawler.stats.get_value("retry/reason_count/foo") == 1
85+
assert crawler.stats.get_value("retry/max_reached") is None
86+
_assert_all_unique_instances(page_instances)
87+
_assert_all_unique_instances(page_response_instances)
88+
89+
5690
@inlineCallbacks
5791
def test_retry_max():
5892
# The default value of the RETRY_TIMES Scrapy setting is 2.

0 commit comments

Comments
 (0)