Skip to content
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
97 changes: 74 additions & 23 deletions PyBugReporter/src/BugReporter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
import sys
import traceback
from functools import wraps
Expand Down Expand Up @@ -93,20 +94,38 @@ def __call__(self, func: callable) -> None:
Args:
func (callable): the function to be decorated
"""
@wraps(func)
def wrapper(*args, **kwargs) -> None:
"""Wrapper function that catches exceptions and sends a bug report to the github repository.

Args:
*args: the arguments for the function
**kwargs: the keyword arguments for the function
"""
repoName = self.repoName
try:
return func(*args, **kwargs)
except Exception as e:
self._handleError(e, repoName, *args, **kwargs)
return wrapper
if inspect.iscoroutinefunction(func):
@wraps(func)
async def wrapper_async(*args, **kwargs) -> None:
"""Wrapper function that catches exceptions and sends a bug report to the github repository.
Works for async functions.

Args:
*args: the arguments for the function
**kwargs: the keyword arguments for the function
"""
repoName = self.repoName
try:
return func(*args, **kwargs)
except Exception as e:
await self._handleError_async(e, repoName, *args, **kwargs)
return wrapper_async
else:
@wraps(func)
def wrapper(*args, **kwargs) -> None:
"""Wrapper function that catches exceptions and sends a bug report to the github repository.
Works for synchronous functions.

Args:
*args: the arguments for the function
**kwargs: the keyword arguments for the function
"""
repoName = self.repoName
try:
return func(*args, **kwargs)
except Exception as e:
self._handleError(e, repoName, *args, **kwargs)
return wrapper

def _handleError(self, e: Exception, repoName: str, *args, **kwargs) -> None:
"""Handles error by creating a bug report.
Expand All @@ -117,6 +136,46 @@ def _handleError(self, e: Exception, repoName: str, *args, **kwargs) -> None:
Raises:
e: the exception that was raised
"""
title, description, shortDescription = self._prepare_bug_report(e, repoName, args, kwargs)

# Check if we need to send a bug report
if not self.handlers[repoName].test:
self._sendBugReport(repoName, title, description, shortDescription)

print(title)
print(description)
raise e

async def _handleError_async(self, e: Exception, repoName: str, *args, **kwargs) -> None:
"""Handles error by creating a bug report asynchronously.

Args:
e (Exception): the exception that was raised

Raises:
e: the exception that was raised
"""
title, description, shortDescription = self._prepare_bug_report(e, repoName, args, kwargs)

# Check if we need to send a bug report
if not self.handlers[repoName].test:
await self._sendBugReport_async(repoName, title, description, shortDescription)

print(title)
print(description)
raise e


def _prepare_bug_report(self, e: Exception, repoName: str, *args, **kwargs) -> tuple[str,str,str]:
"""Prepares all information needed to send the bug report.

Args:
e (Exception): the exception that was raised

Returns:
tuple[str,str,str]: The title, description, and short description of the error for the report.

"""
excType = type(e).__name__
tb = traceback.extract_tb(sys.exc_info()[2])
functionName = tb[-1][2]
Expand Down Expand Up @@ -144,15 +203,7 @@ def _handleError(self, e: Exception, repoName: str, *args, **kwargs) -> None:
shortDescription = f"{start}{compress[:2000 - staticLength]}{end}"

print(f"SHORT DESCRIPTION with length {len(shortDescription)}:\n{shortDescription}")


# Check if we need to send a bug report
if not self.handlers[repoName].test:
self._sendBugReport(repoName, title, description, shortDescription)

print(title)
print(description)
raise e
return title,description,shortDescription

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