Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

source-stripe-native: handle responses for children of deleted resources #1982

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions source-stripe-native/source_stripe_native/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,6 +12,9 @@
)


from estuary_cdk.http import HTTPError


from .models import (
EventResult,
BackfillResult,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion source-stripe-native/source_stripe_native/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}


Expand Down
Loading