diff --git a/source-stripe-native/source_stripe_native/api.py b/source-stripe-native/source_stripe_native/api.py index d4044f41d..4806e9d5d 100644 --- a/source-stripe-native/source_stripe_native/api.py +++ b/source-stripe-native/source_stripe_native/api.py @@ -3,7 +3,7 @@ from estuary_cdk.http import HTTPSession from logging import Logger from typing import Iterable, Any, Callable, Awaitable, AsyncGenerator, Literal -import json +import json, re from estuary_cdk.capture.common import ( BaseDocument, @@ -12,6 +12,9 @@ ) +from estuary_cdk.http import HTTPError + + from .models import ( EventResult, BackfillResult, @@ -26,6 +29,8 @@ API = "https://api.stripe.com/v1" MAX_PAGE_LIMIT = 100 +MISSING_RESOURCE_REGEX = r"resource_missing.+No such.+" + def add_event_types(params: dict[str, str | int], event_types: dict[str, Literal["c", "u", "d"]]): """ Adds the event types (i.e. keys) of the passed in `event_types` dict to a @@ -328,9 +333,21 @@ async def _capture_substreams( while True: if cls_child.NAME == "Persons" and parent_data.controller["requirement_collection"] == "stripe" : break - result_child = ListResult[cls_child].model_validate_json( - await http.request(log, child_url, method="GET", params=parameters) - ) + + try: + result_child = ListResult[cls_child].model_validate_json( + await http.request(log, child_url, method="GET", params=parameters) + ) + except HTTPError as err: + # It's possible for us to process events for deleted parent resources, making + # the requests for the associated child resources fail. Stripe returns a 404 + # error & a message containing "resource_missing" and "No such" when this happens. + if err.code == 404 and bool(re.search(MISSING_RESOURCE_REGEX, err.message, re.DOTALL)): + log.warning(f"Missing resource error for URL {child_url}. Skipping to the next resource.", err) + break + # Propagate all other errors. + else: + raise err for doc in result_child.data: yield doc diff --git a/source-stripe-native/source_stripe_native/models.py b/source-stripe-native/source_stripe_native/models.py index c529b9820..7acbf2cce 100644 --- a/source-stripe-native/source_stripe_native/models.py +++ b/source-stripe-native/source_stripe_native/models.py @@ -428,7 +428,6 @@ class InvoiceLineItems(BaseStripeChildObject): EVENT_TYPES: ClassVar[dict[str, Literal["c", "u", "d"]]] = { "invoice.created": "c", "invoice.updated": "u", - "invoice.deleted": "d", }