Skip to content

Commit

Permalink
Adding in a try/catch for HTTP 409 conflicts on entity creation
Browse files Browse the repository at this point in the history
  • Loading branch information
BryanFauble committed Sep 20, 2024
1 parent 560c369 commit 4c7b2b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
40 changes: 37 additions & 3 deletions synapseclient/api/entity_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
<https://rest-docs.synapse.org/rest/#org.sagebionetworks.repo.web.controller.EntityController>
"""

import asyncio
import json
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from async_lru import alru_cache

from synapseclient.core.exceptions import SynapseHTTPError

if TYPE_CHECKING:
from synapseclient import Synapse

Expand Down Expand Up @@ -36,9 +39,40 @@ async def post_entity(
params = {}
if generated_by:
params["generatedBy"] = generated_by
return await client.rest_post_async(
uri="/entity", body=json.dumps(request), params=params
)

try:
return await client.rest_post_async(
uri="/entity", body=json.dumps(request), params=params
)
except SynapseHTTPError:
if "name" in request and "parentId" in request:
loop = asyncio.get_event_loop()
entity_id = await loop.run_in_executor(
None,
lambda: Synapse.get_client(synapse_client=synapse_client).findEntityId(
name=request["name"],
parent=request["parentId"],
),
)
if not entity_id:
raise

client.logger.warning(
"This is a temporary exception to recieving an HTTP 409 conflict error - Retrieving the state of the object in Synapse. If an error is not printed after this, assume the operation was successful (SYNSD-1233)."
)
existing_instance = await get_entity(
entity_id, synapse_client=synapse_client
)
# Loop over the request params and if any of them do not match the existing instance, raise the error
for key, value in request.items():
if key not in existing_instance or existing_instance[key] != value:
client.logger.error(
f"Failed temporary HTTP 409 logic check because {key} not in instance in Synapse or value does not match: [Existing: {existing_instance[key]}, Expected: {value}]."
)
raise
return existing_instance
else:
raise


async def put_entity(
Expand Down
1 change: 1 addition & 0 deletions synapseclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6299,6 +6299,7 @@ async def _rest_call_async(
Returns:
JSON encoding of response
"""
self.logger.debug(f"Sending {method} request to {uri}")
uri, headers = self._build_uri_and_headers(
uri, endpoint=endpoint, headers=headers
)
Expand Down

0 comments on commit 4c7b2b9

Please sign in to comment.