From c263ad0b03d1f251e7257a64f7dd01d5759750f5 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Tue, 10 Dec 2019 22:20:17 +0000 Subject: [PATCH] Return ClientResponse object from fetch only if the retrieval was successful If an exception was raised and caught, the object is still present in the history. --- qwarc/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/qwarc/__init__.py b/qwarc/__init__.py index 721c91c..968b351 100644 --- a/qwarc/__init__.py +++ b/qwarc/__init__.py @@ -94,12 +94,13 @@ class Item: if response and exc is None and writeToWarc: self.warc.write_client_response(response) history.append((response, exc)) + retResponse = response if exc is None else None if action in (ACTION_SUCCESS, ACTION_IGNORE): - return response, tuple(history) + return retResponse, tuple(history) elif action == ACTION_FOLLOW_OR_SUCCESS: redirectUrl = response.headers.get('Location') or response.headers.get('URI') if not redirectUrl: - return response, tuple(history) + return retResponse, tuple(history) url = url.join(yarl.URL(redirectUrl)) if response.status in (301, 302, 303) and method == 'POST': method = 'GET' @@ -107,7 +108,7 @@ class Item: attempt = 0 elif action == ACTION_RETRIES_EXCEEDED: self.logger.error(f'Request for {url} failed {attempt} times') - return response, tuple(history) + return retResponse, tuple(history) elif action == ACTION_RETRY: # Nothing to do, just go to the next cycle pass