MCPcopy Create free account
hub / github.com/Accenture/mcp-bench / handle_errors

Function handle_errors

utils/error_handler.py:28–73  ·  view source on GitHub ↗

Decorator to standardize error handling according to CLAUDE.md specifications. Args: operation_name: Description of the operation for logging log_level: Logging level for errors (default: ERROR) reraise: Whether to reraise the exception after logging Ret

(
    operation_name: str,
    log_level: int = logging.ERROR,
    reraise: bool = True
)

Source from the content-addressed store, hash-verified

26
27
28def handle_errors(
29 operation_name: str,
30 log_level: int = logging.ERROR,
31 reraise: bool = True
32) -> Callable:
33 """Decorator to standardize error handling according to CLAUDE.md specifications.
34
35 Args:
36 operation_name: Description of the operation for logging
37 log_level: Logging level for errors (default: ERROR)
38 reraise: Whether to reraise the exception after logging
39
40 Returns:
41 Decorated function with error handling
42 """
43 def decorator(func: Callable) -> Callable:
44 @functools.wraps(func)
45 async def async_wrapper(*args, **kwargs) -> Any:
46 try:
47 return await func(*args, **kwargs)
48 except Exception as e:
49 logger.log(log_level, f"ERROR in {operation_name}: {e}")
50 logger.log(log_level, f"Full traceback: {traceback.format_exc()}")
51 if reraise:
52 raise
53 return None
54
55 @functools.wraps(func)
56 def sync_wrapper(*args, **kwargs) -> Any:
57 try:
58 return func(*args, **kwargs)
59 except Exception as e:
60 logger.log(log_level, f"ERROR in {operation_name}: {e}")
61 logger.log(log_level, f"Full traceback: {traceback.format_exc()}")
62 if reraise:
63 raise
64 return None
65
66 # Return appropriate wrapper based on function type
67 import inspect
68 if inspect.iscoroutinefunction(func):
69 return async_wrapper
70 else:
71 return sync_wrapper
72
73 return decorator
74
75
76def log_exception(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected