Skip to content

Commit a9f4ce3

Browse files
authored
Dev (#81)
2 parents c83e884 + 4068b85 commit a9f4ce3

2 files changed

Lines changed: 75 additions & 24 deletions

File tree

PyBugReporter/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.0.12b1'
1+
__version__ = '1.0.12b1.dev1'

PyBugReporter/src/BugReporter.py

Lines changed: 74 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import inspect
23
import sys
34
import traceback
45
from functools import wraps
@@ -93,20 +94,38 @@ def __call__(self, func: callable) -> None:
9394
Args:
9495
func (callable): the function to be decorated
9596
"""
96-
@wraps(func)
97-
def wrapper(*args, **kwargs) -> None:
98-
"""Wrapper function that catches exceptions and sends a bug report to the github repository.
99-
100-
Args:
101-
*args: the arguments for the function
102-
**kwargs: the keyword arguments for the function
103-
"""
104-
repoName = self.repoName
105-
try:
106-
return func(*args, **kwargs)
107-
except Exception as e:
108-
self._handleError(e, repoName, *args, **kwargs)
109-
return wrapper
97+
if inspect.iscoroutinefunction(func):
98+
@wraps(func)
99+
async def wrapper_async(*args, **kwargs) -> None:
100+
"""Wrapper function that catches exceptions and sends a bug report to the github repository.
101+
Works for async functions.
102+
103+
Args:
104+
*args: the arguments for the function
105+
**kwargs: the keyword arguments for the function
106+
"""
107+
repoName = self.repoName
108+
try:
109+
return func(*args, **kwargs)
110+
except Exception as e:
111+
await self._handleError_async(e, repoName, *args, **kwargs)
112+
return wrapper_async
113+
else:
114+
@wraps(func)
115+
def wrapper(*args, **kwargs) -> None:
116+
"""Wrapper function that catches exceptions and sends a bug report to the github repository.
117+
Works for synchronous functions.
118+
119+
Args:
120+
*args: the arguments for the function
121+
**kwargs: the keyword arguments for the function
122+
"""
123+
repoName = self.repoName
124+
try:
125+
return func(*args, **kwargs)
126+
except Exception as e:
127+
self._handleError(e, repoName, *args, **kwargs)
128+
return wrapper
110129

111130
def _handleError(self, e: Exception, repoName: str, *args, **kwargs) -> None:
112131
"""Handles error by creating a bug report.
@@ -117,6 +136,46 @@ def _handleError(self, e: Exception, repoName: str, *args, **kwargs) -> None:
117136
Raises:
118137
e: the exception that was raised
119138
"""
139+
title, description, shortDescription = self._prepare_bug_report(e, repoName, args, kwargs)
140+
141+
# Check if we need to send a bug report
142+
if not self.handlers[repoName].test:
143+
self._sendBugReport(repoName, title, description, shortDescription)
144+
145+
print(title)
146+
print(description)
147+
raise e
148+
149+
async def _handleError_async(self, e: Exception, repoName: str, *args, **kwargs) -> None:
150+
"""Handles error by creating a bug report asynchronously.
151+
152+
Args:
153+
e (Exception): the exception that was raised
154+
155+
Raises:
156+
e: the exception that was raised
157+
"""
158+
title, description, shortDescription = self._prepare_bug_report(e, repoName, args, kwargs)
159+
160+
# Check if we need to send a bug report
161+
if not self.handlers[repoName].test:
162+
await self._sendBugReport_async(repoName, title, description, shortDescription)
163+
164+
print(title)
165+
print(description)
166+
raise e
167+
168+
169+
def _prepare_bug_report(self, e: Exception, repoName: str, *args, **kwargs) -> tuple[str,str,str]:
170+
"""Prepares all information needed to send the bug report.
171+
172+
Args:
173+
e (Exception): the exception that was raised
174+
175+
Returns:
176+
tuple[str,str,str]: The title, description, and short description of the error for the report.
177+
178+
"""
120179
excType = type(e).__name__
121180
tb = traceback.extract_tb(sys.exc_info()[2])
122181
functionName = tb[-1][2]
@@ -144,15 +203,7 @@ def _handleError(self, e: Exception, repoName: str, *args, **kwargs) -> None:
144203
shortDescription = f"{start}{compress[:2000 - staticLength]}{end}"
145204

146205
print(f"SHORT DESCRIPTION with length {len(shortDescription)}:\n{shortDescription}")
147-
148-
149-
# Check if we need to send a bug report
150-
if not self.handlers[repoName].test:
151-
self._sendBugReport(repoName, title, description, shortDescription)
152-
153-
print(title)
154-
print(description)
155-
raise e
206+
return title,description,shortDescription
156207

157208
def _sendBugReport(self, repoName: str, errorTitle: str, errorMessage: str, shortErrorMessage: str) -> None:
158209
"""Sends a bug report to the Github repository.

0 commit comments

Comments
 (0)