File: errors.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (42 lines) | stat: -rw-r--r-- 2,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class CannotOverwriteExistingCassetteException(Exception):
    def __init__(self, *args, **kwargs):
        self.cassette = kwargs["cassette"]
        self.failed_request = kwargs["failed_request"]
        message = self._get_message(kwargs["cassette"], kwargs["failed_request"])
        super(CannotOverwriteExistingCassetteException, self).__init__(message)

    @staticmethod
    def _get_message(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)
        if best_matches:
            # Build a comprehensible message to put in the exception.
            best_matches_msg = "Found {} similar requests with {} different matcher(s) :\n".format(
                len(best_matches), len(best_matches[0][2])
            )

            for idx, best_match in enumerate(best_matches, start=1):
                request, succeeded_matchers, failed_matchers_assertion_msgs = best_match
                best_matches_msg += (
                    "\n%s - (%r).\n"
                    "Matchers succeeded : %s\n"
                    "Matchers failed :\n" % (idx, request, succeeded_matchers)
                )
                for failed_matcher, assertion_msg in failed_matchers_assertion_msgs:
                    best_matches_msg += "%s - assertion failure :\n" "%s\n" % (failed_matcher, assertion_msg)
        else:
            best_matches_msg = "No similar requests, that have not been played, found."
        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):
    """Raised when a cassette does not contain the request we want."""

    pass