#5078·scrapy

spider_error signal is not called on an exception in DownloaderMiddlewares

Author: adsdtCreated Apr 4, 2021Updated Sep 26, 2026
Labelsenhancement

Description

The spider_error signal is not called when receiving an exception from DownloaderMiddleware. This is different from similar behavior for other scrapy components. I have not found any other way to handle the exception from DownloaderMiddleware using signal or standard methods. The goal is to handle an exception after all DownloaderMiddleware.process_exception methods return None. When you raise an error in DownloaderMiddleware.process_response - process_exception is not called, how to handle such errors?

Steps to Reproduce

  1. add a spider
from scrapy import Request, Spider, signals


def get_import_full_name(subject):
    if hasattr(subject, "__name__"):
        return ".".join([subject.__module__, subject.__name__])
    return ".".join([subject.__module__, subject.__class__.__name__])


class ExampleDownloaderMiddleware:
    def process_request(self, request, spider):
        raise Exception('custom exception on custom middleware')

    def process_response(self, request, response, spider):
        return response

    def process_exception(self, request, exception, spider):
        return None


class ExampleSpider(Spider):
    name = 'example'
    custom_settings = {
        'DOWNLOADER_MIDDLEWARES': {
            get_import_full_name(ExampleDownloaderMiddleware): 2,
        },
    }

    @classmethod
    def from_crawler(cls, crawler, *args, **kwargs):
        spider = super().from_crawler(crawler, *args, **kwargs)
        crawler.signals.connect(spider.on_spider_error, signal=signals.spider_error)
        return spider

    def on_item_error(self, failure, response, spider):
        self.logger.error(f'on_item_error: {repr(failure)}')

    def start_requests(self):
        yield Request('https://api.myip.com/', callback=self.parse, dont_filter=True)

    def parse(self, response, **kwargs):
        self.logger.info('spdier.parse')

    def on_spider_error(self, failure, response, spider):
        self.logger.error(repr(f"on_spider_error: {repr(failure)}"))
  1. run the spider
scrapy crawl example

Expected behavior: Calling the spider_error signal. Similar to the behavior when an exception is raised in the following methods:

  • Spider.parse
  • Spider.errback
  • SpiderMiddleware.process_spider_input
  • SpiderMiddleware.process_spider_output call raise Exception() in any of these methods call a spider_error signal.

Actual behavior: [What actually happens] spider_error is not called even if the error has not been handled in DownloaderMiddleware.process_exception.

Reproduces how often: [What percentage of the time does it reproduce?]

  • 100%

Versions

Scrapy : 2.4.1 lxml : 4.5.2.0 libxml2 : 2.9.5 cssselect : 1.1.0 parsel : 1.6.0 w3lib : 1.22.0 Twisted : 20.3.0 Python : 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)] pyOpenSSL : 19.1.0 (OpenSSL 1.1.1g 21 Apr 2020) cryptography : 3.0 Platform : Windows-10-10.0.19041-SP0

Additional context