Skip to content Skip to sidebar Skip to footer

Python/twisted - How To Print More Detailed Error Message

I run this code: import argparse from tqdm import tqdm from sys import argv from pprint import pformat from twisted.internet.task import react from twisted.web.client import Agen

Solution 1:

There's a bug in your error function. The traceback is telling you this:

  File "scanner.py", line 37, in errorprint failure.value.reasons[0].printTraceback()
exceptions.AttributeError: 'exceptions.TypeError' object has no attribute 'reasons'

scanner.py, line 37, in a function named error, the line print failure.value.reasons[0].printTraceback() provokes an AttributeError because a TypeError instance has no reasons attribute.

I think the reasons attribute you're looking belongs to RequestGenerationFailed, RequestTransmissionFailed, or ResponseFailed.

So, define error like this, instead:

from twisted.web._newclient import (
    RequestGenerationFailed, 
    RequestTransmissionFailed,
    ResponseFailed,
)

def error(failure, url):
    f.write("Error: "+url+"\n") 
    if failure.check(
        RequestGenerationFailed,
        RequestTransmissionFailed,
        ResponseFailed,
    ):
        failure.value.reasons[0].printTraceback()
    else:
        failure.printTraceback()

And if this proves useful, file a ticket against Twisted to make those exception types public (since there's no guarantee the _newclient import will continue to work in the future).

Post a Comment for "Python/twisted - How To Print More Detailed Error Message"