Skip to content

Commit 0591787

Browse files
committed
feat(async): Make on_http_error decorator work with coroutines
on_http_error should work with sync functions that return coroutines and handle errors when coroutines will be awaited
1 parent cf8e233 commit 0591787

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

gitlab/exceptions.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
import asyncio
1819
import functools
1920

2021

@@ -262,11 +263,22 @@ def on_http_error(error):
262263

263264
def wrap(f):
264265
@functools.wraps(f)
265-
async def wrapped_f(*args, **kwargs):
266+
def wrapped_f(*args, **kwargs):
266267
try:
267-
return await f(*args, **kwargs)
268+
result = f(*args, **kwargs)
268269
except GitlabHttpError as e:
269270
raise error(e.error_message, e.response_code, e.response_body)
271+
else:
272+
if not asyncio.iscoroutine(result):
273+
return result
274+
275+
async def awaiter():
276+
try:
277+
await result
278+
except GitlabHttpError as e:
279+
raise error(e.error_message, e.response_code, e.response_body)
280+
281+
return awaiter
270282

271283
return wrapped_f
272284

0 commit comments

Comments
 (0)