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

Function handle_errors_with_stats

utils/error_handler.py:380–437  ·  view source on GitHub ↗

Enhanced error handling decorator with statistics tracking. Like handle_errors but also records error statistics for monitoring. Args: operation_name: Description of the operation for logging log_level: Logging level for errors reraise: Whether to reraise th

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

Source from the content-addressed store, hash-verified

378
379
380def handle_errors_with_stats(
381 operation_name: str,
382 log_level: int = logging.ERROR,
383 reraise: bool = True,
384 record_stats: bool = True
385) -> Callable:
386 """Enhanced error handling decorator with statistics tracking.
387
388 Like handle_errors but also records error statistics for monitoring.
389
390 Args:
391 operation_name: Description of the operation for logging
392 log_level: Logging level for errors
393 reraise: Whether to reraise the exception after logging
394 record_stats: Whether to record error statistics
395
396 Returns:
397 Decorated function with error handling and stats
398 """
399 def decorator(func: Callable) -> Callable:
400 @functools.wraps(func)
401 async def async_wrapper(*args, **kwargs) -> Any:
402 try:
403 return await func(*args, **kwargs)
404 except Exception as e:
405 logger.log(log_level, f"ERROR in {operation_name}: {e}")
406 logger.log(log_level, f"Full traceback: {traceback.format_exc()}")
407
408 if record_stats:
409 error_stats.record_error(operation_name, e)
410
411 if reraise:
412 raise
413 return None
414
415 @functools.wraps(func)
416 def sync_wrapper(*args, **kwargs) -> Any:
417 try:
418 return func(*args, **kwargs)
419 except Exception as e:
420 logger.log(log_level, f"ERROR in {operation_name}: {e}")
421 logger.log(log_level, f"Full traceback: {traceback.format_exc()}")
422
423 if record_stats:
424 error_stats.record_error(operation_name, e)
425
426 if reraise:
427 raise
428 return None
429
430 # Return appropriate wrapper based on function type
431 import inspect
432 if inspect.iscoroutinefunction(func):
433 return async_wrapper
434 else:
435 return sync_wrapper
436
437 return decorator

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected