Skip to content

[SYNPY-1581] remove try: except: blocks that log and raise exceptions #1203

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

Merged
merged 1 commit into from
May 15, 2025
Merged
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
57 changes: 20 additions & 37 deletions synapseclient/core/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,17 @@ def f(*args, **kwds):
def wrap_async_to_sync(coroutine: Coroutine[Any, Any, Any], syn: "Synapse") -> Any:
"""Wrap an async function to be called in a sync context."""
loop = None

try:
try:
loop = asyncio.get_running_loop()
except RuntimeError:
pass

if loop:
nest_asyncio.apply(loop=loop)
return loop.run_until_complete(coroutine)
else:
return asyncio.run(coroutine)

except Exception as ex:
syn.logger.exception(
f"Error occurred while running {coroutine} in a sync context."
)
raise ex
loop = asyncio.get_running_loop()
except RuntimeError:
pass

if loop:
nest_asyncio.apply(loop=loop)
return loop.run_until_complete(coroutine)
else:
return asyncio.run(coroutine)


# Adapted from
Expand All @@ -122,28 +116,17 @@ async def wrapper(*args, **kwargs):
return await getattr(self, async_method_name)(*args, **kwargs)

loop = None

try:
try:
loop = asyncio.get_running_loop()
except RuntimeError:
pass

if loop:
nest_asyncio.apply(loop=loop)
return loop.run_until_complete(wrapper(*args, **kwargs))
else:
return asyncio.run(wrapper(*args, **kwargs))

except Exception as ex:
from synapseclient import Synapse

synapse_client = Synapse.get_client(
getattr(kwargs, "synapse_client", None)
)
synapse_client.logger.exception(
f"Error occurred while running {async_method_name} on {self.__class__}."
)
raise ex
loop = asyncio.get_running_loop()
except RuntimeError:
pass

if loop:
nest_asyncio.apply(loop=loop)
return loop.run_until_complete(wrapper(*args, **kwargs))
else:
return asyncio.run(wrapper(*args, **kwargs))

return newmethod

Expand Down
Loading