Skip to content

Commit

Permalink
added fail reason to logging in the service middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewZMSU committed Jun 27, 2024
1 parent ec807dc commit 605a461
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scrapypuppeteer/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import defaultdict
from typing import List, Union
from urllib.parse import urlencode, urljoin
from http import HTTPStatus

from scrapy import Request, signals
from scrapy.crawler import Crawler
Expand Down Expand Up @@ -62,6 +63,8 @@ class PuppeteerServiceDownloaderMiddleware:
SERVICE_META_SETTING = "PUPPETEER_INCLUDE_META"
DEFAULT_INCLUDE_HEADERS = ["Cookie"] # TODO send them separately

service_logger = logging.getLogger(__name__)

def __init__(
self,
crawler: Crawler,
Expand Down Expand Up @@ -177,6 +180,9 @@ def process_response(self, request, response, spider):
response_cls = self._get_response_class(puppeteer_request.action)

if response.status != 200:
reason = response_data.pop("error", f"undefined, status {response.status}")
self.service_logger.warning(f"Request {request} is not succeeded. "
f"Reason: {reason}")
context_id = response_data.get("contextId")
if context_id:
self.used_contexts[id(spider)].add(context_id)
Expand Down

0 comments on commit 605a461

Please sign in to comment.