From 28d9899b9b144aac1d83a7ab619402f5e010628e Mon Sep 17 00:00:00 2001 From: Arthur Hamon Date: Wed, 12 Jun 2019 13:24:13 +0200 Subject: [PATCH] refactor the 'CannotOverwriteExistingCassetteException' exception, building a more detailed message The 'CannotOverwriteExistingCassetteException' exception now takes two kwargs, cassette and failed requests, in order to get the request(s) in the cassettes with the less differences and put those details in the exception message. --- vcr/errors.py | 27 ++++++++++++++++++++++++++- vcr/stubs/__init__.py | 7 ++----- vcr/stubs/tornado_stubs.py | 6 ++---- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/vcr/errors.py b/vcr/errors.py index bdc9701..f762e8c 100644 --- a/vcr/errors.py +++ b/vcr/errors.py @@ -1,5 +1,30 @@ class CannotOverwriteExistingCassetteException(Exception): - pass + def __init__(self, *args, **kwargs): + message = self._get_message(kwargs["cassette"], kwargs["failed_request"]) + super(CannotOverwriteExistingCassetteException, self).__init__(message) + + def _get_message(self, cassette, failed_request): + """Get the final message related to the exception""" + # Get the similar requests in the cassette that + # have match the most with the request. + best_matches = cassette.find_requests_with_most_matches(failed_request) + # Build a comprehensible message to put in the exception. + best_matches_msg = "" + for best_match in best_matches: + request, _, failed_matchers_assertion_msgs = best_match + best_matches_msg += "Similar request found : (%r).\n" % request + for failed_matcher, assertion_msg in failed_matchers_assertion_msgs: + best_matches_msg += "Matcher failed : %s\n" "%s\n" % ( + failed_matcher, + assertion_msg, + ) + return ( + "Can't overwrite existing cassette (%r) in " + "your current record mode (%r).\n" + "No match for the request (%r) was found.\n" + "%s" + % (cassette._path, cassette.record_mode, failed_request, best_matches_msg) + ) class UnhandledHTTPRequestError(KeyError): diff --git a/vcr/stubs/__init__.py b/vcr/stubs/__init__.py index c01ba59..05490bb 100644 --- a/vcr/stubs/__init__.py +++ b/vcr/stubs/__init__.py @@ -230,11 +230,8 @@ class VCRConnection(object): self._vcr_request ): raise CannotOverwriteExistingCassetteException( - "No match for the request (%r) was found. " - "Can't overwrite existing cassette (%r) in " - "your current record mode (%r)." - % (self._vcr_request, self.cassette._path, - self.cassette.record_mode) + cassette=self.cassette, + failed_request=self._vcr_request ) # Otherwise, we should send the request, then get the response diff --git a/vcr/stubs/tornado_stubs.py b/vcr/stubs/tornado_stubs.py index b675065..5beea4b 100644 --- a/vcr/stubs/tornado_stubs.py +++ b/vcr/stubs/tornado_stubs.py @@ -75,10 +75,8 @@ def vcr_fetch_impl(cassette, real_fetch_impl): request, 599, error=CannotOverwriteExistingCassetteException( - "No match for the request (%r) was found. " - "Can't overwrite existing cassette (%r) in " - "your current record mode (%r)." - % (vcr_request, cassette._path, cassette.record_mode) + cassette=cassette, + failed_request=vcr_request ), request_time=self.io_loop.time() - request.start_time, )