Skip to content

Error

Error classes for Pact.

Attributes

logger = logging.getLogger(__name__) module-attribute

Classes

BodyMismatch(path: str, expected: str, actual: str, mismatch: str)

Bases: Mismatch

Mismatch due to an incorrect body element.

PARAMETER DESCRIPTION
path

Path expression to where the mismatch occurred.

TYPE: str

expected

Expected value.

TYPE: str

actual

Actual value.

TYPE: str

mismatch

Description of the mismatch.

TYPE: str

Source code in src/pact/v3/error.py
def __init__(
    self,
    path: str,
    expected: str,
    actual: str,
    mismatch: str,
) -> None:
    """
    Initialise a new BodyMismatch.

    Args:
        path:
            Path expression to where the mismatch occurred.

        expected:
            Expected value.

        actual:
            Actual value.

        mismatch:
            Description of the mismatch.
    """
    self._path = path
    self._expected = expected
    self._actual = actual
    self._mismatch = mismatch

Attributes

actual: str property

Actual value.

expected: str property

Expected value.

mismatch: str property

Description of the mismatch.

path: str property

Path expression to where the mismatch occurred.

type: str property

Type of the mismatch.

BodyTypeMismatch(expected: str, actual: str, mismatch: str, expected_body: bytes | None = None, expectedBody: bytes | None = None, actual_body: bytes | None = None, actualBody: bytes | None = None)

Bases: Mismatch

Mismatch due to an incorrect content type of the body.

PARAMETER DESCRIPTION
expected

Expected content type of the body.

TYPE: str

actual

Actual content type of the body.

TYPE: str

mismatch

Description of the mismatch.

TYPE: str

expected_body

Expected body content.

TYPE: bytes | None DEFAULT: None

actual_body

Actual body content.

TYPE: bytes | None DEFAULT: None

expectedBody

Alias for expected_body.

TYPE: bytes | None DEFAULT: None

actualBody

Alias for actual_body.

TYPE: bytes | None DEFAULT: None

Source code in src/pact/v3/error.py
def __init__(  # noqa: PLR0913
    self,
    expected: str,
    actual: str,
    mismatch: str,
    expected_body: bytes | None = None,
    expectedBody: bytes | None = None,  # noqa: N803
    actual_body: bytes | None = None,
    actualBody: bytes | None = None,  # noqa: N803
) -> None:
    """
    Initialise a new BodyTypeMismatch.

    Args:
        expected:
            Expected content type of the body.

        actual:
            Actual content type of the body.

        mismatch:
            Description of the mismatch.

        expected_body:
            Expected body content.

        actual_body:
            Actual body content.

        expectedBody:
            Alias for `expected_body`.

        actualBody:
            Alias for `actual_body`.
    """
    self._expected = expected
    self._actual = actual
    self._mismatch = mismatch
    self._expected_body = expected_body or expectedBody
    self._actual_body = actual_body or actualBody

Attributes

actual: str property

Actual content type of the body.

actual_body: bytes | None property

Actual body content.

expected: str property

Expected content type of the body.

expected_body: bytes | None property

Expected body content.

mismatch: str property

Description of the mismatch.

type: str property

Type of the mismatch.

GenericMismatch(**kwargs: Any)

Bases: Mismatch

Generic mismatch between the Pact contract and the actual interaction.

This is used when the mismatch is not otherwise covered by a specific mismatch below.

PARAMETER DESCRIPTION
kwargs

Data for the mismatch.

TYPE: Any DEFAULT: {}

Source code in src/pact/v3/error.py
def __init__(self, **kwargs: Any) -> None:  # noqa: ANN401
    """
    Initialise a new GenericMismatch.

    Args:
        kwargs:
            Data for the mismatch.
    """
    self._data = kwargs

Attributes

type: str property

Type of the mismatch.

HeaderMismatch(key: str, expected: str, actual: str, mismatch: str)

Bases: Mismatch

Mismatch due to an incorrect header.

PARAMETER DESCRIPTION
key

Header key.

TYPE: str

expected

Expected value of the header.

TYPE: str

actual

Actual value of the header.

TYPE: str

mismatch

Description of the mismatch.

TYPE: str

Source code in src/pact/v3/error.py
def __init__(self, key: str, expected: str, actual: str, mismatch: str) -> None:
    """
    Initialise a new HeaderMismatch.

    Args:
        key:
            Header key.

        expected:
            Expected value of the header.

        actual:
            Actual value of the header.

        mismatch:
            Description of the mismatch.
    """
    self._key = key
    self._expected = expected
    self._actual = actual
    self._mismatch = mismatch

Attributes

actual: str property

Actual value of the header.

expected: str property

Expected value of the header.

key: str property

Header key.

mismatch: str property

Description of the mismatch.

type: str property

Type of the mismatch.

InteractionVerificationError(description: str, error: Exception)

Bases: PactError

Exception raised due during the verification of an interaction.

This error is raised when an error occurs during the manual verification of an interaction. This is typically raised when the consumer fails to handle the interaction correctly thereby generating its own exception. The cause of the error is stored in the error attribute.

PARAMETER DESCRIPTION
description

Description of the interaction that failed verification.

TYPE: str

error

Error that occurred during the verification of the interaction.

TYPE: Exception

Source code in src/pact/v3/error.py
def __init__(self, description: str, error: Exception) -> None:
    """
    Initialise a new InteractionVerificationError.

    Args:
        description:
            Description of the interaction that failed verification.

        error: Error that occurred during the verification of the
            interaction.
    """
    super().__init__(f"Error verifying interaction '{description}': {error}")
    self._description = description
    self._error = error

Attributes

description: str property

Description of the interaction that failed verification.

error: Exception property

Error that occurred during the verification of the interaction.

MetadataMismatch(key: str, expected: str, actual: str, mismatch: str)

Bases: Mismatch

Mismatch due to incorrect message metadata.

PARAMETER DESCRIPTION
key

Metadata key.

TYPE: str

expected

Expected value.

TYPE: str

actual

Actual value.

TYPE: str

mismatch

Description of the mismatch.

TYPE: str

Source code in src/pact/v3/error.py
def __init__(self, key: str, expected: str, actual: str, mismatch: str) -> None:
    """
    Initialise a new MetadataMismatch.

    Args:
        key:
            Metadata key.

        expected:
            Expected value.

        actual:
            Actual value.

        mismatch:
            Description of the mismatch.
    """
    self._key = key
    self._expected = expected
    self._actual = actual
    self._mismatch = mismatch

Attributes

actual: str property

Actual value.

expected: str property

Expected value.

key: str property

Metadata key.

mismatch: str property

Description of the mismatch.

type: str property

Type of the mismatch.

MethodMismatch(expected: str, actual: str)

Bases: Mismatch

Mismatch due to an incorrect HTTP method.

PARAMETER DESCRIPTION
expected

Expected HTTP method.

TYPE: str

actual

Actual HTTP method.

TYPE: str

Source code in src/pact/v3/error.py
def __init__(self, expected: str, actual: str) -> None:
    """
    Initialise a new MethodMismatch.

    Args:
        expected:
            Expected HTTP method.

        actual:
            Actual HTTP method.
    """
    self._expected = expected
    self._actual = actual

Attributes

actual: str property

Actual HTTP method.

expected: str property

Expected HTTP method.

type: str property

Type of the mismatch.

Mismatch

Bases: ABC

A mismatch between the Pact contract and the actual interaction.

See https://github.com/pact-foundation/pact-reference/blob/f5ddf3d353149ae0fb539a1616eeb8544509fdfc/rust/pact_matching/src/lib.rs#L880 for the underlying source of the data.

Attributes

type: str abstractmethod property

Type of the mismatch.

Functions

from_dict(data: dict[str, Any]) -> Mismatch classmethod

Create a new Mismatch from a dictionary.

PARAMETER DESCRIPTION
data

Data for the mismatch.

TYPE: dict[str, Any]

RETURNS DESCRIPTION
Mismatch

A new Mismatch object.

Source code in src/pact/v3/error.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> Mismatch:  # noqa: C901, PLR0911
    """
    Create a new Mismatch from a dictionary.

    Args:
        data:
            Data for the mismatch.

    Returns:
        A new Mismatch object.
    """
    if mismatch_type := data.pop("type"):
        # Pact mismatches
        if mismatch_type in ["MissingRequest", "missing-request"]:
            return MissingRequest(**data)
        if mismatch_type in ["RequestNotFound", "request-not-found"]:
            return RequestNotFound(**data)
        if mismatch_type in ["RequestMismatch", "request-mismatch"]:
            return RequestMismatch(**data)

        # Interaction mismatches
        if mismatch_type in ["MethodMismatch", "method-mismatch"]:
            return MethodMismatch(**data)
        if mismatch_type in ["PathMismatch", "path-mismatch"]:
            return PathMismatch(**data)
        if mismatch_type in ["StatusMismatch", "status-mismatch"]:
            return StatusMismatch(**data)
        if mismatch_type in ["QueryMismatch", "query-mismatch"]:
            return QueryMismatch(**data)
        if mismatch_type in ["HeaderMismatch", "header-mismatch"]:
            return HeaderMismatch(**data)
        if mismatch_type in ["BodyTypeMismatch", "body-type-mismatch"]:
            return BodyTypeMismatch(**data)
        if mismatch_type in ["BodyMismatch", "body-mismatch"]:
            return BodyMismatch(**data)
        if mismatch_type in ["MetadataMismatch", "metadata-mismatch"]:
            return MetadataMismatch(**data)
            logger.warning("RequestMismatch not implemented")

        logger.warning("Unknown mismatch type: %s (%r)", mismatch_type, data)
        return GenericMismatch(**data, type=mismatch_type)
    return GenericMismatch(**data)

MismatchesError(*mismatches: Mismatch | dict[str, Any])

Bases: PactError

Exception raised when there are mismatches between the Pact and the server.

PARAMETER DESCRIPTION
mismatches

Mismatches between the Pact and the server.

TYPE: Mismatch | dict[str, Any] DEFAULT: ()

Source code in src/pact/v3/error.py
def __init__(self, *mismatches: Mismatch | dict[str, Any]) -> None:
    """
    Initialise a new MismatchesError.

    Args:
        mismatches:
            Mismatches between the Pact and the server.
    """
    super().__init__(f"Mismatched interaction (count: {len(mismatches)})")
    self._mismatches = [
        m if isinstance(m, Mismatch) else Mismatch.from_dict(m) for m in mismatches
    ]

Attributes

mismatches: list[Mismatch] property

Mismatches between the Pact and the server.

MissingRequest(method: str, path: str, request: dict[str, Any])

Bases: Mismatch

Mismatch due to a missing request.

PARAMETER DESCRIPTION
method

HTTP method of the missing request.

TYPE: str

path

Path of the missing request.

TYPE: str

request

Details of the missing request.

TYPE: dict[str, Any]

Source code in src/pact/v3/error.py
def __init__(self, method: str, path: str, request: dict[str, Any]) -> None:
    """
    Initialise a new MissingRequest.

    Args:
        method:
            HTTP method of the missing request.

        path:
            Path of the missing request.

        request:
            Details of the missing request.
    """
    self._method = method
    self._path = path
    self._request = request

Attributes

method: str property

HTTP method of the missing request.

path: str property

Path of the missing request.

request: dict[str, Any] property

Details of the missing request.

type: str property

Type of the mismatch.

PactError

Bases: Exception, ABC

Base class for exceptions raised by the Pact module.

PactVerificationError(errors: list[InteractionVerificationError])

Bases: PactError

Exception raised due to errors in the verification of a Pact.

This is raised when performing manual verification of the Pact through the verify method:

pact = Pact("consumer", "provider")
# Define interactions...
try:
    pact.verify(handler, kind="Async")
except PactVerificationError as e:
    print(e.errors)

All of the errors that occurred during the verification of all of the interactions are stored in the errors attribute.

This is different from the [MismatchesError][pact.v3.MismatchesError] which is raised when there are mismatches detected by the mock server.

PARAMETER DESCRIPTION
errors

Errors that occurred during the verification of the Pact.

TYPE: list[InteractionVerificationError]

Source code in src/pact/v3/error.py
def __init__(self, errors: list[InteractionVerificationError]) -> None:
    """
    Initialise a new PactVerificationError.

    Args:
        errors:
            Errors that occurred during the verification of the Pact.
    """
    super().__init__(f"Error verifying Pact (count: {len(errors)})")
    self._errors = errors

Attributes

errors: list[InteractionVerificationError] property

Errors that occurred during the verification of the Pact.

PathMismatch(expected: str, actual: str, mismatch: str)

Bases: Mismatch

Mismatch due to an incorrect path.

PARAMETER DESCRIPTION
expected

Expected path.

TYPE: str

actual

Actual path.

TYPE: str

mismatch

Mismatch between the expected and actual paths.

TYPE: str

Source code in src/pact/v3/error.py
def __init__(self, expected: str, actual: str, mismatch: str) -> None:
    """
    Initialise a new PathMismatch.

    Args:
        expected:
            Expected path.

        actual:
            Actual path.

        mismatch:
            Mismatch between the expected and actual paths.
    """
    self._expected = expected
    self._actual = actual
    self._mismatch = mismatch

Attributes

actual: str property

Actual path.

expected: str property

Expected path.

mismatch: str property

Mismatch between the expected and actual paths.

type: str property

Type of the mismatch.

QueryMismatch(parameter: str, expected: str, actual: str, mismatch: str)

Bases: Mismatch

Mismatch due to an incorrect query parameter.

PARAMETER DESCRIPTION
parameter

Query parameter name.

TYPE: str

expected

Expected value of the query parameter.

TYPE: str

actual

Actual value of the query parameter.

TYPE: str

mismatch

Description of the mismatch.

TYPE: str

Source code in src/pact/v3/error.py
def __init__(
    self,
    parameter: str,
    expected: str,
    actual: str,
    mismatch: str,
) -> None:
    """
    Initialise a new QueryMismatch.

    Args:
        parameter:
            Query parameter name.

        expected:
            Expected value of the query parameter.

        actual:
            Actual value of the query parameter.

        mismatch:
            Description of the mismatch.
    """
    self._parameter = parameter
    self._expected = expected
    self._actual = actual
    self._mismatch = mismatch

Attributes

actual: str property

Actual value of the query parameter.

expected: str property

Expected value of the query parameter.

mismatch: str property

Description of the mismatch.

parameter: str property

Query parameter name.

type: str property

Type of the mismatch.

RequestMismatch(method: str, path: str, mismatches: list[dict[str, Any]])

Bases: Mismatch

Mismatch due to an incorrect request.

PARAMETER DESCRIPTION
method

HTTP method of the request.

TYPE: str

path

Path of the request.

TYPE: str

mismatches

List of mismatches in the request.

TYPE: list[dict[str, Any]]

Source code in src/pact/v3/error.py
def __init__(
    self, method: str, path: str, mismatches: list[dict[str, Any]]
) -> None:
    """
    Initialise a new RequestMismatch.

    Args:
        method:
            HTTP method of the request.

        path:
            Path of the request.

        mismatches:
            List of mismatches in the request.
    """
    self._method = method
    self._path = path
    self._mismatches = [Mismatch.from_dict(m) for m in mismatches]

Attributes

method: str property

HTTP method of the request.

mismatches: list[Mismatch] property

List of mismatches in the request.

path: str property

Path of the request.

type: str property

Type of the mismatch.

RequestNotFound(method: str, path: str, request: dict[str, Any])

Bases: Mismatch

Mismatch due to a request not being found.

PARAMETER DESCRIPTION
method

HTTP method of the request not found.

TYPE: str

path

Path of the request not found.

TYPE: str

request

Details of the request not found.

TYPE: dict[str, Any]

Source code in src/pact/v3/error.py
def __init__(self, method: str, path: str, request: dict[str, Any]) -> None:
    """
    Initialise a new RequestNotFound.

    Args:
        method:
            HTTP method of the request not found.

        path:
            Path of the request not found.

        request:
            Details of the request not found.
    """
    self._method = method
    self._path = path
    self._request = request

Attributes

method: str property

HTTP method of the request not found.

path: str property

Path of the request not found.

request: dict[str, Any] property

Details of the request not found.

type: str property

Type of the mismatch.

StatusMismatch(expected: int, actual: int, mismatch: str)

Bases: Mismatch

Mismatch due to an incorrect HTTP status code.

PARAMETER DESCRIPTION
expected

Expected HTTP status code.

TYPE: int

actual

Actual HTTP status code.

TYPE: int

mismatch

Description of the mismatch.

TYPE: str

Source code in src/pact/v3/error.py
def __init__(self, expected: int, actual: int, mismatch: str) -> None:
    """
    Initialise a new StatusMismatch.

    Args:
        expected:
            Expected HTTP status code.

        actual:
            Actual HTTP status code.

        mismatch:
            Description of the mismatch.
    """
    self._expected = expected
    self._actual = actual
    self._mismatch = mismatch

Attributes

actual: int property

Actual HTTP status code.

expected: int property

Expected HTTP status code.

mismatch: str property

Description of the mismatch.

type: str property

Type of the mismatch.