question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

raise CloseSpider("Error Message") is not saving error message

See original GitHub issue

When I run the code raise CloseSpider("Error Message") in the Spider parse function, the reason gets saved in the finish_reason stats string.

When it is run in the start_requests function, the spider properly closes but the finish_reason does not have the message it in. The finish_reason value is “finished” instead of “Error Message” as set in the CloseSpider exception.

Is this intentional or a bug? thanks!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
novilunicommented, Nov 26, 2020

If someone needs a workaround to raise a CloseSpider with a specific message in the start_requests method before the fix is released, we did something like this:

def start_requests(self):
    try:
        # whatever you need to do 
    except:
        yield Request(url='file:///dev/null', callback=self.force_fail, errback=self.force_fail)
        return


def force_fail(self, response):
    raise CloseSpider(reason='failed')

Of course, you can change the try/except by a if/else or whatever you want and the “url” by any other URL. The “magic” here occurs when calling the callback, as the reason of the CloseSpider will be respected.

1reaction
elacuestacommented, Sep 11, 2019

@Gallaecio I beg to differ, but I don’t think this is a bug, since the docs do say the exception “can be raised from a spider callback”. Any change to allow raising the exception from other components should be considered an enhancement IMHO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

handle scrapy exception CloseSpider - python - Stack Overflow
2019-08-10 14:55:56 [scrapy.utils.signal] ERROR: Error caught on signal ... in _open raise CloseSpider(reason='no_file') scrapy.exceptions.
Read more >
Exceptions — Scrapy 2.7.1 documentation
This exception can be raised from a spider callback to request the spider to be closed/stopped. Supported arguments: Parameters. reason (str) – ...
Read more >
Python scrapy.exceptions.DropItem() Examples
REQUIRED_RE.search(error.message) if required_match: ... if not spider.item_filter(item): raise DropItem('Custom item filter did not allow ... Not saving.
Read more >
[Example code]-Scrapy spider not terminating with use of ...
From reading the Scrapy docs I identified I need to use the CloseSpider extension. I have followed the guide on how to enable...
Read more >
for notable python repositories - issuebase
raise CloseSpider ("Error Message") is not saving error message. 10/15/2018. enhancementgood first issue · [bug] pillow will always recode images in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found