Skip to content

Commit 6a4e858

Browse files
authored
Merge pull request #1203 from Sage-Bionetworks/SYNPY-1581-async-exception-handling
remove try: except: blocks that log and raise exceptions in async wrappers
2 parents 1ed5b6e + 9144f31 commit 6a4e858

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

synapseclient/core/async_utils.py

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,17 @@ def f(*args, **kwds):
8080
def wrap_async_to_sync(coroutine: Coroutine[Any, Any, Any], syn: "Synapse") -> Any:
8181
"""Wrap an async function to be called in a sync context."""
8282
loop = None
83+
8384
try:
84-
try:
85-
loop = asyncio.get_running_loop()
86-
except RuntimeError:
87-
pass
88-
89-
if loop:
90-
nest_asyncio.apply(loop=loop)
91-
return loop.run_until_complete(coroutine)
92-
else:
93-
return asyncio.run(coroutine)
94-
95-
except Exception as ex:
96-
syn.logger.exception(
97-
f"Error occurred while running {coroutine} in a sync context."
98-
)
99-
raise ex
85+
loop = asyncio.get_running_loop()
86+
except RuntimeError:
87+
pass
88+
89+
if loop:
90+
nest_asyncio.apply(loop=loop)
91+
return loop.run_until_complete(coroutine)
92+
else:
93+
return asyncio.run(coroutine)
10094

10195

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

124118
loop = None
119+
125120
try:
126-
try:
127-
loop = asyncio.get_running_loop()
128-
except RuntimeError:
129-
pass
130-
131-
if loop:
132-
nest_asyncio.apply(loop=loop)
133-
return loop.run_until_complete(wrapper(*args, **kwargs))
134-
else:
135-
return asyncio.run(wrapper(*args, **kwargs))
136-
137-
except Exception as ex:
138-
from synapseclient import Synapse
139-
140-
synapse_client = Synapse.get_client(
141-
getattr(kwargs, "synapse_client", None)
142-
)
143-
synapse_client.logger.exception(
144-
f"Error occurred while running {async_method_name} on {self.__class__}."
145-
)
146-
raise ex
121+
loop = asyncio.get_running_loop()
122+
except RuntimeError:
123+
pass
124+
125+
if loop:
126+
nest_asyncio.apply(loop=loop)
127+
return loop.run_until_complete(wrapper(*args, **kwargs))
128+
else:
129+
return asyncio.run(wrapper(*args, **kwargs))
147130

148131
return newmethod
149132

0 commit comments

Comments
 (0)